Python中http請求方法庫匯總
最近在使用python做接口測試,發(fā)現(xiàn)python中http請求方法有許多種,今天抽點時間把相關(guān)內(nèi)容整理,分享給大家,具體內(nèi)容如下所示:
一、python自帶庫----urllib2
python自帶庫urllib2使用的比較多,簡單使用如下:
import urllib2 response = urllib2.urlopen('http://localhost:8080/jenkins/api/json?pretty=true') print response.read()
簡單的get請求
import urllib2 import urllib post_data = urllib.urlencode({}) response = urllib2.urlopen('http://localhost:8080/, post_data) print response.read() print response.getheaders()
這就是最簡單的urllib2發(fā)送post例子。代碼比較多
二、python自帶庫--httplib
httplib是一個相對底層的http請求模塊,urlib就是基于httplib封裝的。簡單使用如下:
import httplib conn = httplib.HTTPConnection("www.python.org") conn.request("GET", "/index.html") r1 = conn.getresponse() print r1.status, r1.reason data1 = r1.read() conn.request("GET", "/parrot.spam") r2 = conn.getresponse() data2 = r2.read() conn.close()
簡單的get請求
我們再來看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() data = response.read() print data conn.close()
是不是覺得太復(fù)雜了。每次寫還得再翻文檔,看看第三種吧
三、第三方庫--requests
發(fā)請get請求超級簡單:
print requests.get('http://localhost:8080).text
就一句話,再來看看post請求
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload) print r.text
也很簡單。
再看看如果要認(rèn)證:
url = 'http://localhost:8080' r = requests.post(url, data={}, auth=HTTPBasicAuth('admin', 'admin')) print r.status_code print r.headers print r.reason
是不是比urllib2更簡單多了吧,且requests自帶json解析。這點非常棒
python中的http請求
import urllib params = urllib.urlencode({key:value,key:value}) resultHtml = urllib.urlopen('[API or 網(wǎng)址]',params) result = resultHtml.read() print result
- Python基于httpx模塊實現(xiàn)發(fā)送請求
- Python爬蟲實現(xiàn)HTTP網(wǎng)絡(luò)請求多種實現(xiàn)方式
- Python3自定義http/https請求攔截mitmproxy腳本實例
- 解決Python發(fā)送Http請求時,中文亂碼的問題
- 如何基于Python + requests實現(xiàn)發(fā)送HTTP請求
- python用requests實現(xiàn)http請求代碼實例
- Python使用指定端口進(jìn)行http請求的例子
- 對Python發(fā)送帶header的http請求方法詳解
- 利用python的socket發(fā)送http(s)請求方法示例
- Python發(fā)送http請求解析返回json的實例
- Python Http請求json解析庫用法解析
相關(guān)文章
pandas數(shù)據(jù)拼接的實現(xiàn)示例
這篇文章主要介紹了pandas數(shù)據(jù)拼接的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04使用Python微信庫itchat獲得好友和群組已撤回的消息
這篇文章主要介紹了使用Python微信庫itchat獲得好友和群組已撤回的消息,需要的朋友可以參考下2018-06-06使用 Python 合并多個格式一致的 Excel 文件(推薦)
這篇文章主要介紹了使用 Python 合并多個格式一致的 Excel 文件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12使用python進(jìn)行文本預(yù)處理和提取特征的實例
今天小編就為大家分享一篇使用python進(jìn)行文本預(yù)處理和提取特征的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python字節(jié)單位轉(zhuǎn)換(將字節(jié)轉(zhuǎn)換為K M G T)
這篇文章主要介紹了Python字節(jié)單位轉(zhuǎn)換(將字節(jié)轉(zhuǎn)換為K M G T),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03利用Python對中國500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析
這篇文章主要介紹了利用Python對中國500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析,從不同角度去對數(shù)據(jù)進(jìn)行統(tǒng)計分析,可視化展示,下文詳細(xì)內(nèi)容介紹需要的小伙伴可以參考一下2022-05-05