2010-05-05 42 views
2

我必須向服務器發送請求。在該網站的API文檔有這個例子中,使用捲曲在PHP中:幫助在Python中使用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的工作轉換嗎?

謝謝!

+0

您是否嘗試過使用簡單的「http://www.google.com」?這真的應該工作......你看到了什麼問題? – 2010-05-05 21:21:59

+0

因此切斷了http的URL – 2010-05-05 21:24:04

+1

@Ldn如果您想要一個很好的答案,請包括您正在編寫程序的方式失敗的描述。沒有錯誤信息很難診斷您的問題。 – 2010-05-06 14:42:20

回答

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。 有什麼想法?

非常感謝!

+0

嘗試向urlopen中添加'data =「」'以將其從'GET'更改爲'POST'而沒有數據。 – 2013-10-30 12:49:00

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

Python代碼使用的urllib2看起來不錯,應該工作。可能在你沒有提到的其他東西中存在錯誤;你能請更具體嗎?

+0

是的,但它是原生的嗎? – elledienne 2010-05-05 21:18:39

+0

原生?你問是否在標準的Python庫中?不它不是。 – Messa 2010-05-05 21:20:22

+0

我試圖安裝pycurl,但我得到這個錯誤,典型的mac: i686-apple-darwin10-gcc-4.2.1:/usr/lib/libcurl.a:沒有這樣的文件或目錄 powerpc-apple-darwin10 -gcc-4.2.1:/usr/lib/libcurl.a:沒有這樣的文件或目錄i686-apple-darwin10-gcc-4.2.1:/ usr/lib/libcurl。a:沒有這樣的文件或directorylipo:無法弄清楚:/var/tmp//ccf74ZBD.out – elledienne 2010-05-05 22:21:07

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' 

你得到的錯誤是什麼?

+0

的架構類型,我得到-BAD_REQUEST(400),這意味着請求格式不正確或提交不正確。 但我敢肯定,請求是正確的格式!所以問題出在 – elledienne 2010-05-05 22:16:35

+0

你確定你的數據是正確的,編碼等?您的API有特殊要求嗎? 沒有具體的信息,很難幫助你確定你的問題。 – 2010-05-06 22:17:09

1

考慮使用數據包嗅探器來判斷cURL是否正在發送用戶代理信息。如果是,服務期待的信息,然後使用add_header()方法對你Request(從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