Python 中的 cURL 帮助

发布于 2024-08-31 10:31:27 字数 676 浏览 6 评论 0原文

我必须向服务器发送请求。在该网站的 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?

谢谢你!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-09-07 10:31:27

curl 也适用于 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 代码看起来不错,它应该可以工作。可能您没有提到的其他内容有错误;您能说得更具体一些吗?

curl is for Python too: http://pycurl.sourceforge.net/

The example could be translated into Python and pycurl like this:

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()

Your Python code using urllib2 looks OK, it should be working. Probably there is an error in something other you did not mention in question; could you be please more specific?

忆梦 2024-09-07 10:31:27

考虑使用数据包嗅探器来确定 cURL 是否正在发送用户代理信息。如果是,并且服务需要该信息,则在 请求(来自 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

Consider using a packet sniffer to figure out if cURL is sending User-Agent information. If it is, and the service is expecting that information, then use the add_header() method on your Request (from urllib2 documentation, bottom of page):

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
荒岛晴空 2024-09-07 10:31:27

考虑你的代码,这实际上应该有效。

 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'

您遇到的错误是什么?

Taking your code, this should actually work.

 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'

What is the error you are getting?

左秋 2024-09-07 10:31:27

这很尴尬,但是...我使用 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。
有什么想法吗?

非常感谢!!

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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文