本文介绍了在Python中帮助cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须向服务器发送请求。在网站的API文档中,有一个在PHP中使用cURL的示例:

  $ ch = curl_init 
curl_setopt($ ch,CURLOPT_URL,'http://api.website.com');
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,request = $ wrapper);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
;
$ data = curl_exec($ ch);
curl_close($ ch);

但是我的应用程序是使用Python完成的,所以我试图写这样的东西, t work:

  req = urllib2.Request(url,formatted)
response = urllib2.urlopen(req)
html = response.read()
print html +\\\
\\\

你能帮我把PHP cURL程序的工作转换写成Python吗?



谢谢!

这是很尴尬,但...我唯一的问题,我的代码使用urllib和urllib2是...这个代码做一个GET,而不是一个POST! p>

这里我使用Wireshark进行扫描:



1-使用urllib和urllib2

 超文本传输​​协议
GET / HTTP / 1.1 \r\\\

[聊天/序列]:GET / HTTP / 1.1 \r\\\
]
[消息:GET / HTTP / 1.1\r\\\
]
[严重级别:聊天]
[组:序列]
请求方法:GET
请求URI:/
请求版本:HTTP / 1.1
接受编码:identity \r\\\

主机:api.apptrackr.org\r \\\

连接:close\r\\\

用户代理:Python-urllib / 2.6\r\\\

\r\\\



2-使用PyCurl

 超文本传输​​协议
POST / HTTP / 1.1 \r\\\

[专家信息(聊天/序列):POST / HTTP / 1.1\r\\\
]
[消息:POST / HTTP / 1.1 \r\\\
]
[严重级别:聊天]
[组:序列]
请求方法:POST
请求URI:/
请求版本:HTTP / 1.1
用户代理:PycURL / 7.19.5\r\\\

主机:api.website.com\r\\\

接受:* / * \r\\\

Content-Length:365 \r\\\

[Content length:365]
Content-Type:application / x-www-form -urlencoded \r\\\

\r\\\

基于行的文本数据:application / x-www-form-urlencoded
[truncated] request =%7B%22enc_key %22%3A%22o37vOsNetKgprRE0VsBYefYViP4%2ByB3pjxfkfCYtpgiQ%2ByxONgkhhsxtqAwaXwCrrgx%2BPDuDtMRZNI1ez // 4Zw%3D%3D%22%2C%22format%22%3A%22RSA_RC4_Sealed%22%2C%22profile%22%3A%22Ldn%22%2C%22request% 22%3A%22bQ%2BHm /

所以代码工作,但它不适合我,需要一个POST,但我会更喜欢使用NOT PyCurl。
任何想法?



非常感谢!


I have to POST a request to a server. In the API documentation of the website there is this example that uses cURL in PHP:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.website.com');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=$wrapper");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
;
$data = curl_exec($ch);
curl_close($ch);

But my app is done using Python, so I tried to write something like that but this code doesn't work:

req = urllib2.Request(url, formatted)  
response = urllib2.urlopen(req)  
html = response.read()  
print html+"\n\n"  

Can you help me write a working conversion of a PHP cURL program to Python?

Thank you!!

解决方案

It's quite embarrassing but... the only problem with my code that uses urllib and urllib2 is... that this code do a GET and not a POST !!!

Here my scan using Wireshark:

1- using urllib and urllib2

Hypertext Transfer Protocol
    GET / HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): GET / HTTP/1.1\r\n]
            [Message: GET / HTTP/1.1\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Method: GET
        Request URI: /
        Request Version: HTTP/1.1
    Accept-Encoding: identity\r\n
    Host: api.apptrackr.org\r\n
    Connection: close\r\n
    User-Agent: Python-urllib/2.6\r\n
    \r\n

2- using PyCurl

Hypertext Transfer Protocol
    POST / HTTP/1.1\r\n
        [Expert Info (Chat/Sequence): POST / HTTP/1.1\r\n]
            [Message: POST / HTTP/1.1\r\n]
            [Severity level: Chat]
            [Group: Sequence]
        Request Method: POST
        Request URI: /
        Request Version: HTTP/1.1
    User-Agent: PycURL/7.19.5\r\n
    Host: api.website.com\r\n
    Accept: */*\r\n
    Content-Length: 365\r\n
        [Content length: 365]
    Content-Type: application/x-www-form-urlencoded\r\n
    \r\n
Line-based text data: application/x-www-form-urlencoded
    [truncated]         request=%7B%22enc_key%22%3A%22o37vOsNetKgprRE0VsBYefYViP4%2ByB3pjxfkfCYtpgiQ%2ByxONgkhhsxtqAwaXwCrrgx%2BPDuDtMRZNI1ez//4Zw%3D%3D%22%2C%22format%22%3A%22RSA_RC4_Sealed%22%2C%22profile%22%3A%22Ldn%22%2C%22request%22%3A%22bQ%2BHm/

so the code works, but it's not right for me because i need a POST, but i will prefer to use NOT PyCurl.Any idea?

Thank you very much!!

这篇关于在Python中帮助cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:38