11.1 我们需要以客户端的形式通过HTTP歇息访问多种服务。
- 使用urllib.request模块通常就可以满足要求。
from urllib import request,parse
url = 'http://httpbin.org/get'
parms = {
'name1':'value1',
'name2':'value2'
}
qs = parse.urlencode(parms)
u = request.urlopen(url+"?"+qs)
resp = u.read()
print(resp)
~/ning-dashboard/tmp > python3 ttt.py
b'{\n "args": {\n "name1": "value1", \n "name2": "value2"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.7", \n "X-Amzn-Trace-Id": "Root=1-6298de82-639ab39566b89bc35bd45c7a"\n }, \n "origin": "210.22.74.111", \n "url": "http://httpbin.org/get?name1=value1&name2=value2"\n}\n'
- 如果需要使用POST方法在请求主题中发送查询参数,可以将参数编码后作为可选参数提供给urlopen()函数
from urllib import request,parse
url = 'http://httpbin.org/post'
parms = {
'name1':'value1',
'name2':'value2'
}
qs = parse.urlencode(parms)
u = request.urlopen(url,qs.encode("ascii"))
resp = u.read()
print(resp)
~/ning-dashboard/tmp > python3 ttt.py 13s
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "name1": "value1", \n "name2": "value2"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "25", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.7", \n "X-Amzn-Trace-Id": "Root=1-6298df7a-572e0ce9697610826d3e8807"\n }, \n "json": null, \n "origin": "210.22.74.111", \n "url": "http://httpbin.org/post"\n}\n'
- 如果想在发出的请求中提供一些自定义的http头,比如修改user-agent字段,那么可以创建一个包含字段值的字典,并创建一个request实例,然后将其传给urlopen(),
from urllib import request,parse
url = 'http://httpbin.org/post'
parms = {
'name1':'value1',
'name2':'value2'
}
headers ={
'User-agent':'none/ofyourbusiness',
'Spam':'Eggs'
}
qs = parse.urlencode(parms)
req = request.Request(url,qs.encode("ascii"),headers=headers)
u = request.urlopen(req)
resp = u.read()
print(resp)
~/ning-dashboard/tmp > python3 ttt.py 2m 18s
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "name1": "value1", \n "name2": "value2"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "25", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "Spam": "Eggs", \n "User-Agent": "none/ofyourbusiness", \n "X-Amzn-Trace-Id": "Root=1-6298e1cb-2cec587f0550b28d35dff7ec"\n }, \n "json": null, \n "origin": "210.22.74.111", \n "url": "http://httpbin.org/post"\n}\n'
import requests
url = 'http://httpbin.org/post'
parms = {
'name1':'value1',
'name2':'value2'
}
headers ={
'User-agent':'none/ofyourbusiness',
'Spam':'Eggs'
}
resp = requests.post(url,data=parms,headers=headers)
print(resp.text)
~/ning-dashboard/tmp > python3 ttt.py 20s
{
"args": {},
"data": "",
"files": {},
"form": {
"name1": "value1",
"name2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "25",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"Spam": "Eggs",
"User-Agent": "none/ofyourbusiness",
"X-Amzn-Trace-Id": "Root=1-6298e122-393530d343acb327131bcca8"
},
"json": null,
"origin": "210.22.74.111",
"url": "http://httpbin.org/post"
}
网友评论