use*_*569 243 python urllib httplib
我在网上找到了这个脚本:
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()
Run Code Online (Sandbox Code Playgroud)
但我不明白如何使用它与PHP或params变量内的所有内容或如何使用它.试着让它发挥作用,我可以请一点帮助吗?
小智 359
如果你真的想用Python处理HTTP,我强烈推荐Requests:HTTP for Humans.适用于您的问题的POST快速入门是:
>>> 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...
>>>
Run Code Online (Sandbox Code Playgroud)
sti*_*til 139
如果你需要你的脚本是可移植的,而你宁愿没有任何第三方依赖,那么这就是你纯粹在Python 3中发送POST请求的方式.
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)
Run Code Online (Sandbox Code Playgroud)
样本输出:
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
d1j*_*i1b 35
您无法使用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
Run Code Online (Sandbox Code Playgroud)
例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"
},
...
}
Run Code Online (Sandbox Code Playgroud)
例1.3:
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> r = requests.post(url, data=json.dumps(payload))
Run Code Online (Sandbox Code Playgroud)
requests
通过点击REST API端点,使用库进行GET,POST,PUT或DELETE。在中传递其余api端点url,在中传递url
有效载荷(dict),data
并在中传递头/元数据headers
import requests, json
url = "bugs.python.org"
payload = {"number": 12524,
"type": "issue",
"action": "show"}
header = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
response_decoded_json = requests.post(url, data=payload, headers=header)
response_json = response_decoded_json.json()
print response_json
Run Code Online (Sandbox Code Playgroud)
小智 6
您的数据字典包含表单输入字段的名称,您只需保持正确的值即可找到结果。 表单视图 标题配置浏览器以检索您声明的数据类型。使用 requests 库可以轻松发送 POST:
import requests
url = "https://bugs.python.org"
data = {'@number': 12524, '@type': 'issue', '@action': 'show'}
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"}
response = requests.post(url, data=data, headers=headers)
print(response.text)
Run Code Online (Sandbox Code Playgroud)
更多关于请求对象:https : //requests.readthedocs.io/en/master/api/