1、使用requests执行
requests库在Python 2和Python 3中的用法基本一致,但在响应内容的类型上有细微差别:在Python 3中,response.text 返回 str 类型(Unicode文本),而 response.content 返回 bytes 类型;在Python 2中,response.text 返回 unicode 类型,response.content 则是原始的 str(即字节串)。这一区别主要源于两种Python版本对字符串的本质处理方式不同。
相关文档:https://2.python-requests.org//zh_CN/latest/user/quickstart.html
import requests
url = 'https://...'
payload = {'key1': 'value1', 'key2': 'value2'}
# GET
r = requests.get(url)
# GET with params in URL
r = requests.get(url, params=payload)
# POST with form-encoded data
r = requests.post(url, data=payload)
# POST with JSON
import json
r = requests.post(url, data=json.dumps(payload))
# Response, status etc
r.text
r.status_code
2、使用urllib2执行
Python 2中常用的 urllib2 模块在Python 3中被拆分为 urllib.request 和 urllib.error 两个模块,用于处理请求和异常。当将Python 2代码迁移到Python 3时,2to3 工具会自动将 import urllib2 转换为相应的 urllib.request 和 urllib.error 导入,以适配新版标准库的结构变更。
相关文档:https://docs.python.org/2/library/urllib2.html
def URLRequest(url, params, method="GET"):
if method == "POST":
return urllib2.Request(url, data=urllib.urlencode(params))
else:
return urllib2.Request(url + "?" + urllib.urlencode(params))
3、使用httplib2执行
httplib2 支持缓存、重定向、压缩等功能,与 requests 相比,httplib2 更轻量但稍显底层,适合需要高级控制的场景。
相关文档:
https://github.com/jcgregorio/httplib2
https://github.com/httplib2/httplib2/wiki/Examples
https://github.com/httplib2/httplib2/wiki/Examples-Python3
1) Python2中执行
#!/usr/bin/env python
import urllib
import httplib2
http = httplib2.Http()
url = 'http://www.example.com/login'
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST',
headers=headers, body=urllib.urlencode(body))
headers = {'Cookie': response['set-cookie']}
url = 'http://www.example.com/home'
response, content = http.request(url, 'GET', headers=headers)2) Python3中执行
#!/usr/bin/python3
import urllib.parse
import httplib2
http = httplib2.Http()
url = 'http://www.example.com/login'
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST',
headers=headers, body=urllib.parse.urlencode(body))
headers = {'Cookie': response['set-cookie']}
url = 'http://www.example.com/home'
response, content = http.request(url, 'GET', headers=headers)4、Python3使用http.client执行
http.client 是标准库中的低层 HTTP 客户端,适合需要手动构建请求的场景。相比之下,requests 更适合快速开发,而 http.client 更适合底层控制和调试。
相关文档:https://docs.python.org/3/library/http.client.html
#!/usr/bin/env python3
import http.client
import json
print("\n GET example")
conn = http.client.HTTPSConnection("httpbin.org")
conn.request("GET", "/get")
response = conn.getresponse()
data = response.read().decode('utf-8')
print(response.status, response.reason)
print(data)
print("\n POST example")
conn = http.client.HTTPSConnection('httpbin.org')
headers = {'Content-type': 'application/json'}
post_body = {'text': 'testing post'}
json_data = json.dumps(post_body)
conn.request('POST', '/post', json_data, headers)
response = conn.getresponse()
print(response.read().decode())
print(response.status, response.reason)
print("\n PUT example ")
conn = http.client.HTTPSConnection('httpbin.org')
headers = {'Content-type': 'application/json'}
post_body ={'text': 'testing put'}
json_data = json.dumps(post_body)
conn.request('PUT', '/put', json_data, headers)
response = conn.getresponse()
print(response.read().decode(), response.reason)
print(response.status, response.reason)
print("\n delete example")
conn = http.client.HTTPSConnection('httpbin.org')
headers = {'Content-type': 'application/json'}
post_body ={'text': 'testing delete'}
json_data = json.dumps(post_body)
conn.request('DELETE', '/delete', json_data, headers)
response = conn.getresponse()
print(response.read().decode(), response.reason)
print(response.status, response.reason)