Python自動化測試 (九)urllib2 發送HTTP Request


urllib2 是Python自帶的標准模塊, 用來發送HTTP Request的。  類似於 .NET中的,  HttpWebRequest類

 

urllib2 的優點

Python urllib2 發出的HTTP Request, 能自動被Fiddler截獲, 方便了調試。

Python 可以自動處理Cookie

 

urllib2 的缺點

Python urllib2 發出的http Request, 中的header 會被修改成“首字母大寫”,

比如你的代碼里寫的header 是: content-TYPE=application/x-www-form-urlencoded ,  會被修改為 Content-Type=application/x-www-form-urlencoded

 

實例一,  Get方法, 並且自定義header

 

# -* - coding: UTF-8 -* -  
import urllib2

request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()

 

實例二, post方法

 

# -* - coding: UTF-8 -* -  
import urllib2
import urllib

request = urllib2.Request("http://passport.cnblogs.com/login.aspx")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"}

response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()

 

實例三: Cookie 的處理

 

# -* - coding: UTF-8 -* -  
import urllib2
import urllib
import cookielib

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

request = urllib2.Request("https://dynamic.12306.cn/otsweb/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"}

response = opener.open(request, urllib.urlencode(data))

# send again, you will see cookie sent to web server
response = opener.open(request, urllib.urlencode(data))

print response.getcode()
print response.geturl()
print response.read()

 

實例四:如何處理跳轉

創建Opener時, ul2.HTTPRedirectHandler是默認被加上的handler之一 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM