包含curl的–header轉換為pythonrequerts的詞條

本文目錄一覽:

怎麼把這個 curl 命令用 python requests 搞定

import httplib

import json

p = { “method” : “test”, “params” : [ { “detail” : “Hello_world”} ] }

headers = {“Content-type”:”application/x-www-form-urlencoded”,”Accept”:”text/plain”}

conn = httplib.HTTPConnection(“127.0.0.1”, 8081)

conn.request(“POST”,””, json.dumps(p), headers)

如何利用cURL和python對服務端和web端進行接口測試

templateclass NameType, class DistType

int GraphNameType, DistType::GetVertexPosTable(const NameType vertexname)

{

for (int i=0; i this-m_numVertexs; i++)

{

if (vertexname == m_pVertexTable[i].m_vertexName)

{

return i;

}

}

return -1;

}

RCurl-入門1

Term Project需要做一個爬蟲-Crawler。爬什麼、怎麼爬,都不確定。索性網上搜教程開始學。很多語言都可以實現這個功能,比如 Java 、 Python 、 R 這三個我感興趣的語言。

今晚看到的 教學視頻 是關於R的。

R 的爬蟲Package為RCurl,首先需要在RStudio或R上安裝,然後新建 R Script 後,引用該庫。

今晚兩小時,主題是: RCurl 最重要的三個函數。只看到了第一個的兩個基本命令。

首先看一個很基本的查詢網頁是否存在的命令。

當網頁存在是返回 TRUE ,否則返回 FALSE 。

第二個基本命令可以查詢Header。

其中的 verbose=TRUE 參數表示是否要將結果存儲在d中。 d 由 debugGatherer 賦予了三個method,分別是 update 、 value 、 reset 。當需要請求Header信息時,採用 update 函數,將信息存儲在 value 中,如果需要重置 value ,則可使用 reset 。同時,如果 verbose=FALSE ,則會發現 value 中不會存儲此次操作的信息。

PHP的curl模塊和python的pycurl模塊的區別

C的curl:

#include stdio.h

#include curl/curl.h

int main(void)

{

CURL *curl;

CURLcode res;

curl = curl_easy_init();

if(curl) {

/* First set the URL that is about to receive our POST. This URL can

just as well be a https:// URL if that is what should receive the

data. */

curl_easy_setopt(curl, CURLOPT_URL, “”);

/* Now specify the POST data */

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “name=danielproject=curl”);

/* Perform the request, res will get the return code */

res = curl_easy_perform(curl);

/* always cleanup */

curl_easy_cleanup(curl);

}

return ;

}

php的curl:

?php

$c = curl_init();

curl_setopt($c, CURLOPT_URL, ”);

$data = curl_exec($c);

curl_close($c);

echo $c;

?

python的pycurl:

import pycurl

def body(buffer):

print buffer

c = pycurl.Curl()

c.setopt(pycurl.URL, “”)

c.setopt(pycurl.WRITEFUNCTION, body)

c.perform()

主要原理:

C:

使用到的數據結構:

typedef void CURL; /*當初始化什麼的時候只是一個void類型*/

struct SessionHandle {

struct Names dns;

struct Curl_multi *multi; /* 用於多線程處理*/

struct Curl_one_easy *multi_pos; /* if non-NULL, points to the its position

in multi controlling structure to assist

in removal. */

struct Curl_share *share; /* Share, handles global variable mutexing */

struct HandleData reqdata; /* Request-specific data */

struct UserDefined set; /* values set by the libcurl user ,用於setopt等*/

struct DynamicStatic change; /* possibly modified userdefined data */

struct CookieInfo *cookies; /* the cookies, read from files and servers */

struct Progress progress; /* for all the progress meter data */

struct UrlState state; /* struct for fields used for state info and

other dynamic purposes */

struct PureInfo info; /* stats, reports and info data */

#if defined(CURL_DOES_CONVERSIONS) defined(HAVE_ICONV)

iconv_t outbound_cd; /* for translating to the network encoding */

iconv_t inbound_cd; /* for translating from the network encoding */

iconv_t utf8_cd; /* for translating to UTF8 */

#endif /* CURL_DOES_CONVERSIONS HAVE_ICONV */

unsigned int magic; /* set to a CURLEASY_MAGIC_NUMBER */

};

struct UserDefined {

FILE *err; /* the stderr user data goes here */

void *debugdata; /* the data that will be passed to fdebug */

char *errorbuffer; /* (Static) store failure messages in here */

long proxyport; /* If non-zero, use this port number by default. If the

proxy string features a “:[port]” that one will override

this. */

 /**一下省略10000行- -**/

};

使用的方法1:

.初始化curl,得到sessionhandler結構體空間

CURL *curl_easy_init(void)

{

CURLcode res;

struct SessionHandle *data;

/* Make sure we inited the global SSL stuff */

if (!initialized) {

res = curl_global_init(CURL_GLOBAL_DEFAULT);

if(res) {

/* something in the global init failed, return nothing */

DEBUGF(fprintf(stderr, “Error: curl_global_init failedn”));

return NULL;

}

}

/* We use curl_open() with undefined URL so far */

res = Curl_open(data);

if(res != CURLE_OK) {

DEBUGF(fprintf(stderr, “Error: Curl_open failedn”));

return NULL;

}

return data;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/275857.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 16:06
下一篇 2024-12-17 16:06

相關推薦

發表回復

登錄後才能評論