2012-07-04 244 views
134

我發現這個腳本在線:如何發送POST請求?

import httplib, urllib 
params = urllib.urlencode({'number': 12524, 'type': 'issue', 'action': 'show'}) 
headers = {"Content-type": "application/x-www-form-urlencoded", 
      "Accept": "text/plain"} 
conn = httplib.HTTPConnection("bugs.python.org") 
conn.request("POST", "", params, headers) 
response = conn.getresponse() 
print response.status, response.reason 
302 Found 
data = response.read() 
data 
'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>' 
conn.close() 

但我不明白如何使用PHP或者是什麼的一切PARAMS變量中或如何使用它使用它。我可以請一點點幫助,試圖讓這個工作?

+1

發送請求只是發佈請求,無論服務器端是什麼。 –

+7

這發送一個POST請求。然後服務器以302(重定向)標題響應您的POST。究竟是什麼錯誤? – ddinchev

+1

這個腳本看起來不像python3.2 compat – jdi

回答

214

如果你真的想用Python處理HTTP,我強烈建議Requests: HTTP for Humans。適用於您的問題POST quickstart是:

>>> import requests 
>>> r = requests.post("http://bugs.python.org", data={'number': 12524, 'type': 'issue', 'action': 'show'}) 
>>> print(r.status_code, r.reason) 
200 OK 
>>> print(r.text[:300] + '...') 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<title> 
Issue 12524: change httplib docs POST example - Python tracker 

</title> 
<link rel="shortcut i... 
>>> 
+0

我無法獲得與上述相同的結果。我在頁面上寫了另一個問題編號,然後運行腳本,但是我看不到結果中的問題編號。 –

+1

請更改data = {'number':12524,讀取data = {'number':'12524',.我會自己改變它,但編輯需要超過6個字符。感謝 – kevthanewversi

+0

備案:最近爲我工作的代碼,data = {'@ number':'12524','@type':'issue','@action':'show'} – marr

15

使用urllib(僅適用於GET)無法實現POST請求,而不是使用requests模塊嘗試,如:

例1.0:

import requests 

base_url="www.server.com" 
final_url="/{0}/friendly/{1}/url".format(base_url,any_value_here) 

payload = {'number': 2, 'value': 1} 
response = requests.post(final_url, data=payload) 

print(response.text) #TEXT/HTML 
print(response.status_code, response.reason) #HTTP 

實施例1.2:

>>> import requests 

>>> payload = {'key1': 'value1', 'key2': 'value2'} 

>>> r = requests.post("http://httpbin.org/post", data=payload) 
>>> print(r.text) 
{ 
    ... 
    "form": { 
    "key2": "value2", 
    "key1": "value1" 
    }, 
    ... 
} 

例1.3:

>>> import json 

>>> url = 'https://api.github.com/some/endpoint' 
>>> payload = {'some': 'data'} 

>>> r = requests.post(url, data=json.dumps(payload)) 
81

如果你需要你的腳本是便攜式的,你寧可不要任何第三方的依賴,這是你如何在Python 3

from urllib.parse import urlencode 
from urllib.request import Request, urlopen 

url = 'https://httpbin.org/post' # Set destination URL here 
post_fields = {'foo': 'bar'}  # Set POST fields here 

request = Request(url, urlencode(post_fields).encode()) 
json = urlopen(request).read().decode() 
print(json) 
純粹發送POST請求

採樣輸出:

{ 
    "args": {}, 
    "data": "", 
    "files": {}, 
    "form": { 
    "foo": "bar" 
    }, 
    "headers": { 
    "Accept-Encoding": "identity", 
    "Content-Length": "7", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Python-urllib/3.3" 
    }, 
    "json": null, 
    "origin": "127.0.0.1", 
    "url": "https://httpbin.org/post" 
} 
+6

此代碼僅適用於Python 3,就像我在答案中所說的那樣。 – stil