python request 模塊詳細介紹
request
Requests 是使用 Apache2 Licensed 許可證的 基于Python開發的HTTP 庫,其在Python內置模塊的基礎上進行了高度的封裝,從而使得Pythoner進行網絡請求時,變得美好了許多,使用Requests可以輕而易舉的完成瀏覽器可有的任何操作。
GET 請求
# 1、無參數實例 import requests ret = requests.get(’https://github.com/timeline.json’) print ret.urlprint ret.text # 2、有參數實例 import requests payload = {’key1’: ’value1’, ’key2’: ’value2’}ret = requests.get('http://httpbin.org/get', params=payload)print ret.urlprint ret.text
POST 請求
# 1、基本POST實例 import requests payload = {’key1’: ’value1’, ’key2’: ’value2’}ret = requests.post('http://httpbin.org/post', data=payload) print ret.text # 2、發送請求頭和數據實例 import requestsimport json url = ’https://api.github.com/some/endpoint’payload = {’some’: ’data’}headers = {’content-type’: ’application/json’} ret = requests.post(url, data=json.dumps(payload), headers=headers)print ret.textprint ret.cookies
其他請求
requests.get(url, params=None, **kwargs)requests.post(url, data=None, json=None, **kwargs)requests.put(url, data=None, **kwargs)requests.head(url, **kwargs)requests.delete(url, **kwargs)requests.patch(url, data=None, **kwargs)requests.options(url, **kwargs) # 以上方法均是在此方法的基礎上構建requests.request(method, url, **kwargs)
requests.request() 參數
method:提交方式 get/post 。
url:提交地址。
params:在URL上傳遞的參數,GET形式傳遞到后臺,例如向http://www.oldboyyede.com上傳數據。
requests.request( method = ’GET’, url = ’http://www.oldboyyede.com’, params = { ’k1’ : ’v1’ , ’k2’ : ’v2’ } ) # http://www.oldboyyede.com?k1=v1&k2=v2
data:在請求體里面傳遞的數據,后面可以是字典,字節等數據類型。
requests.request( method = ’POST’, url = ’http://www.oldboyyede.com’, # data= { ’k1’ : ’v1’ , ’k2’ : ’v2’ , ’x’:[1,2,3]} data=' user=wjw&pwd=123123 ' )
json:在請求體里面傳遞數據,把整體序列化成一個大字符串,字典中嵌套字典的話用JSON 。
requests.request( method = ’POST’, url = ’http://www.oldboyyede.com’, json= { ’k1’ : ’v1’ , ’k2’ : ’v2’ } ) # '{ ’k1’ : ’v1’ , ’k2’ : ’v2’ }'
headers:請求頭。
一定要添加瀏覽器,不然可能會遇到網絡防火墻
headers = {’User-Agent’: ’Mozilla/5.0 (Windows NT 10.0; WOW64; rv:58.0) Gecko/20100101 Firefox/58.0’}
# 一定要添加瀏覽器,不然可能會遇到網絡防火墻攔截你的請求headers = {’User-Agent’: ’Mozilla/5.0 (Windows NT 10.0; WOW64; rv:58.0) Gecko/20100101 Firefox/58.0’} r1 = requests.get( url=’http://dig.chouti.com/’, headers=headers)
files:上傳文件對象。
# 模塊的詳細使用import requests requests.post( url=’xxxxxx’, files={# ’f1’:open(’s1.py’,’rb’)# 可以上傳元祖的形式上傳,sssss1.py為后臺獲取的名稱’f1’: open(’sssss1.py’,(’s1.py’, ’rb’)) })
參數實例
def param_method_url(): # requests.request(method=’get’, url=’http://127.0.0.1:8000/test/’) # requests.request(method=’post’, url=’http://127.0.0.1:8000/test/’) pass def param_param(): # - 可以是字典 # - 可以是字符串 # - 可以是字節(ascii編碼以內) # requests.request(method=’get’, # url=’http://127.0.0.1:8000/test/’, # params={’k1’: ’v1’, ’k2’: ’水電費’}) # requests.request(method=’get’, # url=’http://127.0.0.1:8000/test/’, # params='k1=v1&k2=水電費&k3=v3&k3=vv3') # requests.request(method=’get’, # url=’http://127.0.0.1:8000/test/’, # params=bytes('k1=v1&k2=k2&k3=v3&k3=vv3', encoding=’utf8’)) # 錯誤 # requests.request(method=’get’, # url=’http://127.0.0.1:8000/test/’, # params=bytes('k1=v1&k2=水電費&k3=v3&k3=vv3', encoding=’utf8’)) pass def param_data(): # 可以是字典 # 可以是字符串 # 可以是字節 # 可以是文件對象 # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # data={’k1’: ’v1’, ’k2’: ’水電費’}) # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # data='k1=v1; k2=v2; k3=v3; k3=v4' # ) # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # data='k1=v1;k2=v2;k3=v3;k3=v4', # headers={’Content-Type’: ’application/x-www-form-urlencoded’} # ) # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # data=open(’data_file.py’, mode=’r’, encoding=’utf-8’), # 文件內容是:k1=v1;k2=v2;k3=v3;k3=v4 # headers={’Content-Type’: ’application/x-www-form-urlencoded’} # ) pass def param_json(): # 將json中對應的數據進行序列化成一個字符串,json.dumps(...) # 然后發送到服務器端的body中,并且Content-Type是 {’Content-Type’: ’application/json’} requests.request(method=’POST’, url=’http://127.0.0.1:8000/test/’, json={’k1’: ’v1’, ’k2’: ’水電費’}) def param_headers(): # 發送請求頭到服務器端 requests.request(method=’POST’, url=’http://127.0.0.1:8000/test/’, json={’k1’: ’v1’, ’k2’: ’水電費’}, headers={’Content-Type’: ’application/x-www-form-urlencoded’} ) def param_cookies(): # 發送Cookie到服務器端 requests.request(method=’POST’, url=’http://127.0.0.1:8000/test/’, data={’k1’: ’v1’, ’k2’: ’v2’}, cookies={’cook1’: ’value1’}, ) # 也可以使用CookieJar(字典形式就是在此基礎上封裝) from http.cookiejar import CookieJar from http.cookiejar import Cookie obj = CookieJar() obj.set_cookie(Cookie(version=0, name=’c1’, value=’v1’, port=None, domain=’’, path=’/’, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={’HttpOnly’: None}, rfc2109=False, port_specified=False, domain_specified=False, domain_initial_dot=False, path_specified=False) ) requests.request(method=’POST’, url=’http://127.0.0.1:8000/test/’, data={’k1’: ’v1’, ’k2’: ’v2’}, cookies=obj) def param_files(): # 發送文件 # file_dict = { # ’f1’: open(’readme’, ’rb’) # } # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # files=file_dict) # 發送文件,定制文件名 # file_dict = { # ’f1’: (’test.txt’, open(’readme’, ’rb’)) # } # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # files=file_dict) # 發送文件,定制文件名 # file_dict = { # ’f1’: (’test.txt’, 'hahsfaksfa9kasdjflaksdjf') # } # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # files=file_dict) # 發送文件,定制文件名 # file_dict = { # ’f1’: (’test.txt’, 'hahsfaksfa9kasdjflaksdjf', ’application/text’, {’k1’: ’0’}) # } # requests.request(method=’POST’, # url=’http://127.0.0.1:8000/test/’, # files=file_dict) pass<br># 做基本認證 header中加密的用戶名和密碼def param_auth(): from requests.auth import HTTPBasicAuth, HTTPDigestAuth ret = requests.get(’https://api.github.com/user’, auth=HTTPBasicAuth(’wangjiawei’, ’sdfasdfasdf’)) print(ret.text) # ret = requests.get(’http://192.168.1.1’, # auth=HTTPBasicAuth(’admin’, ’admin’)) # ret.encoding = ’gbk’ # print(ret.text) # ret = requests.get(’http://httpbin.org/digest-auth/auth/user/pass’, auth=HTTPDigestAuth(’user’, ’pass’)) # print(ret) # # 請求和響應的超時時間def param_timeout(): # ret = requests.get(’http://google.com/’, timeout=1) # print(ret) # ret = requests.get(’http://google.com/’, timeout=(5, 1)) # print(ret) pass<br># 是允許否重定向def param_allow_redirects(): ret = requests.get(’http://127.0.0.1:8000/test/’, allow_redirects=False) print(ret.text)<br># 代理 出口IP 不是本機IP。 流程是:先給代理發,代理幫助我們向目的地址發。def param_proxies(): # proxies = { # 'http': '61.172.249.96:80', # 'https': 'http://61.185.219.126:3128', # } # proxies = {’http://10.20.1.128’: ’http://10.10.1.10:5323’} # ret = requests.get('http://www.proxy360.cn/123456', proxies=proxies) # print(ret.headers) # from requests.auth import HTTPProxyAuth # # proxyDict = { # ’http’: ’77.75.105.165’, # ’https’: ’77.75.105.165’ # } # auth = HTTPProxyAuth(’username’, ’mypassword’) # # r = requests.get('http://www.google.com', proxies=proxyDict, auth=auth) # print(r.text) pass<br># 流形式 比如下片def param_stream(): ret = requests.get(’http://127.0.0.1:8000/test/’, stream=True) print(ret.content) ret.close() # from contextlib import closing # with closing(requests.get(’http://httpbin.org/get’, stream=True)) as r: # # 在此處理響應。迭代處理 # for i in r.iter_content(): # print(i) def requests_session(): import requests session = requests.Session() ### 1、首先登陸任何頁面,獲取cookie i1 = session.get(url='http://dig.chouti.com/help/service') ### 2、用戶登陸,攜帶上一次的cookie,后臺對cookie中的 gpsd 進行授權 i2 = session.post(url='http://dig.chouti.com/login',data={ ’phone’: '8612345678977', ’password’: 'xxxxxx', ’oneMonth’: ''} ) i3 = session.post(url='http://dig.chouti.com/link/vote?linksId=8589623', ) print(i3.text)
http 與 https 區別
http本質上就是 socket,http 請求不安全,因為沒有任何的加密措施。
https擁有加密措施,ssh 加密,有證書一說,服務器給請求的客戶端頒發證書,客戶端使用證書對客戶端發送的數據加密成密文,然后發送給服務器,服務器通過證書進行解密,檢測是否是匹配的,進行傳輸信息。通信間必須使用證書。cert參數是證書,主動提供證書,可以自己制作(缺錢的話),比如證書是“fuck.pem”,cert=(’fuck.pem’),必須使用證書,如果沒有證書則不能進行通信。還有一類證書需要自己買的第三方可信賴的證書,系統在創建(一裝機)的時候直接植入證書,廠商幫忙做驗證,這就是有錢和沒錢的區別,用戶不需要自己在瀏覽器上安裝證書了,pem是證書的格式,cert=(’fuck.crt’,’xxx.key’),和自己辦法的沒區別,需要啥樣給啥樣,完成的事都是做加密使用。還有一個參數叫 verify,如果 verify=False,則表示忽略證書,直接發請求直接拿結果,一般網站是允許用的,但是服務器必須要證書也白搭。
以上就是python request 模塊詳細介紹的詳細內容,更多關于python request 模塊的資料請關注好吧啦網其它相關文章!
相關文章:
