Help with cURL in Python

后端 未结 4 1518
-上瘾入骨i
-上瘾入骨i 2021-01-07 05:05

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         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 05:45

    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
    

提交回复
热议问题