pythonhttplib
1. python3么有httplib了嗎
1、有,python3把httplib改了名字,對應的庫是http.client
2、網址是:
https://docs.python.org/3.4/library/http.client.html
https://docs.python.org/2/library/httplib.html
2. 請教個python httplib2傳遞參數問題
樓主的理解沒有問題啊
.
python中函數的實參傳遞規則是:
標注了參數名的就要按參數名傳遞,打亂順序的情況下一定要加參數名,否則會混亂的。
沒有預設的實參情況下就會依次傳遞,如果不夠的話,後面的會自動去取自己的預設值。
如果實參的數量比
3. 怎麼用python使用pip安裝httplib模塊
直接使用命令安裝即可,以下為安裝命令:
切需到pip所在目錄,執行
pip install httplib
4. python httplib怎麼列印發送的請求
python有一個httplib的庫,提供了很方便的方法實現GET和POST請求,只需要簡單的組織一下即可。
python發送get請求代碼:
#!/usr/bin/env python
#coding=utf8
import httplib
httpClient = None
try:
httpClient = httplib.HTTPConnection('localhost', 80, timeout=30)
httpClient.request('GET', '/test.php')
#response是HTTPResponse對象
response = httpClient.getresponse()
print response.status
print response.reason
print response.read()
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
發送POST請求
#!/usr/bin/env python
#coding=utf8
import httplib, urllib
httpClient = None
try:
params = urllib.urlencode({'name': 'tom', 'age': 22})
headers = {"Content-type": "application/x-www-form-urlencoded"
, "Accept": "text/plain"}
httpClient = httplib.HTTPConnection("localhost", 80, timeout=30)
httpClient.request("POST", "/test.php", params, headers)
response = httpClient.getresponse()
print response.status
print response.reason
print response.read()
print response.getheaders() #獲取頭信息
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
5. python的httplib,urllib和urllib2的區別及用
urllib和urllib2
urllib 和urllib2都是接受URL請求的相關模塊,但是urllib2可以接受一個Request類的實例來設置URL請求的headers,urllib僅可以接受URL。
這意味著,你不可以偽裝你的User Agent字元串等。
urllib提供urlencode方法用來GET查詢字元串的產生,而urllib2沒有。這是為何urllib常和urllib2一起使用的原因。
目前的大部分http請求都是通過urllib2來訪問的
httplib
httplib實現了HTTP和HTTPS的客戶端協議,一般不直接使用,在python更高層的封裝模塊中(urllib,urllib2)使用了它的http實現。
urllib簡單用法
urllib.urlopen(url[, data[, proxies]]) :
[python] view plain
google = urllib.urlopen('http://www.google.com')
print 'http header:/n', google.info()
print 'http status:', google.getcode()
print 'url:', google.geturl()
for line in google: # 就像在操作本地文件
print line,
google.close()
詳細使用方法見
urllib學習
urllib2簡單用法
最簡單的形式
[python] view plain
import urllib2
response=urllib2.urlopen('http://www.douban.com')
html=response.read()
實際步驟:
1、urllib2.Request()的功能是構造一個請求信息,返回的req就是一個構造好的請求
2、urllib2.urlopen()的功能是發送剛剛構造好的請求req,並返回一個文件類的對象response,包括了所有的返回信息。
3、通過response.read()可以讀取到response裡面的html,通過response.info()可以讀到一些額外的信息。