在Python中帮助cURL

时间:2010-05-05 21:08:54

标签: php python post curl libcurl

我必须向服务器发送请求。在网站的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完成的,所以我尝试编写类似的东西,但这段代码不起作用:

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

你能帮我写一个PHP cURL程序到Python的工作转换吗?

谢谢!

4 个答案:

答案 0 :(得分:2)

卷曲也适用于Python:http://pycurl.sourceforge.net/

这个例子可以像这样翻译成Python和pycurl:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://api.website.com")
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, "request=%s" % wrapper)
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
c.close()
data = b.getvalue()

使用urllib2的Python代码看起来不错,应该可以正常工作。可能在你没有提到的其他事情上有错误;你可以更具体一点吗?

答案 1 :(得分:2)

考虑使用数据包嗅探器来确定cURL是否正在发送用户代理信息。如果是,并且服务期望该信息,则使用Request上的add_header()方法(来自urllib2文档,页面底部):

import urllib2
req = urllib2.Request('http://api.website.com/')
# Your parameter encoding here
req.add_header('User-agent', 'Mozilla/5.0')
r = urllib2.urlopen(req)
# Process the response

答案 2 :(得分:1)

接受你的代码,这实际上应该有效。

 import urllib 
 import urllib2

 url = 'http://api.website.com/' 
 values = {'some_key':'some_value'} 
 data = urllib.urlencode(values) 
 req = urllib2.Request(url, data) 
 response = urllib2.urlopen(req) 
 page = response.read()
 print page + '\n\n'

你得到的错误是什么?

答案 3 :(得分:1)

这很令人尴尬但是...我的代码使用urllib和urllib2的唯一问题是......这段代码执行GET而不是POST !!!

我在这里使用Wireshark进行扫描:

1-使用urllib和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-使用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/

所以代码有效,但它不适合我,因为我需要一个POST,但我更喜欢使用NOT PyCurl。 有什么想法吗?

非常感谢!!