httplib和urllib2常用方法

  都是几年前用过的,现在翻出来记录一下。

import httplib
import urllib2
import socket

##--------------------------------------------------------用httplib进行GET请求
conn=httplib.HTTPConnection('172.22.31.40',8080)##如果要走代理,那这里自然要改为代理服务器的地址
conn.request("GET","/cloud/index.php")
result=conn.getresponse()
print result.read()
resultStatus=result.status
print resultStatus
conn.close()

conn=httplib.HTTPConnection('www.cnblogs.com',80)
conn.request("GET","/idbeta/default.html")
result=conn.getresponse()
print result.read()
resultStatus=result.status
print resultStatus
conn.close()

##--------------------------------------------------------用httplib进行POST请求
conn=httplib.HTTPConnection('172.22.131.40',80)
##一般重点关注真实包的"Content-type",例如"application/x-www-form-urlencoded"等;而data则根据真实抓包情况来模拟,需要url编码的用urllib.urlencode
header1 = {"Content-type": "application/x-www-form-urlencoded", "User-Agent" : "test"} 
data1 = '''{
   "audit_control_list" : [],
   "ws" : [
      "base_setting",
   ],
   "xp_fix" : []
} '''
url1='/api/getconf.json?mid=ebcd32d5f68e404db1ccc8ff2dacb360&ver=1.0'
conn.request('POST',url1,body=data1,headers=header1)
result=conn.getresponse()
content=result.read()
print content
conn.close()

##发送multipart/form-data请求的例子
conn=httplib.HTTPConnection('172.22.131.40',80)
header2={"Content-Type":"multipart/form-data; boundary=----------------------------2bb6caed7d98"}
data2 = '''------------------------------2bb6caed7d98
Content-Disposition: form-data; name="em"
md5s=92f44e405db16ac55d97e3bfe3b132fa+04c5d2b4da9a0f3fa8a45702d4256cee42d8c48d	452608	\WINDOWS\syswow64\windowspowershell\v1.0\powershell.exe	1	
dcca4b04af87e52ef9eaa2190e06cbac+12a602b86fc394b1c88348fb099685eabb876495	1174016	\PROGRAM FILES\windows sidebar\sidebar.exe	1	
------------------------------2bb6caed7d98--
'''
url2='/cloudquery.php'
conn.request("POST",url2,body=data2,headers=header2)
result=conn.getresponse()
resultStatus=result.status
##获取请求的页面内容
content=result.read()
print content
print result.status,result.reason
##关闭连接
conn.close()

##--------------------------------------------------------用urllib2进行GET请求
##直接open就是GET
url="http://www.cnblogs.com/idbeta/default.html"
response =urllib2.urlopen(url)
print response.read()
##--------------------------------------------------------用urllib2进行POST请求 url="http://172.22.131.40/api/getconf.json?mid=ebcd32d5f68e404db1ccc8ff2dacb360&ver=1.0" header1 = {"Content-type": "application/x-www-form-urlencoded", "User-Agent" : "test"} data1 = '''{ "audit_control_list" : [], "base_config" : [], "data_linkage" : [], "md" : [], "nac_linkage" : [], "neteye" : [], "p2p_setting" : [], "safe_control_list" : [], "sd" : [ "sd_settings", "rp_settings" ], "ui" : [], "ws" : [ "base_setting", "popwnd_setting", "startup_assistant", "safe_protect", "leak_repair" ], "xp_fix" : [] } ''' req = urllib2.Request(url, data1, header1) # 发送请求同时传data表单,这个是字典方式 response = urllib2.urlopen(req) #接受反馈的信息 the_page = response.read() #读取反馈的内容 print the_page ##--------------------------------------------------------urllib2设置超时时间 ##方法一 全局设置 socket.setdefaulttimeout(1);秒 ##或 urllib2.socket.setdefaulttimeout(1) ##方法二 urllib2.urlopen加入timeout参数 urllib2.urlopen(url,timeout=1) ##--------------------------------------------------------urllib2设置代理 proxy = urllib2.ProxyHandler({"http" : 'http://172.22.31.85:808'}) urllib2.install_opener(urllib2.build_opener(proxy)) url="http://www.cnblogs.com/idbeta/default.html" response =urllib2.urlopen(url) print response.read()

  除了上面所说的,还有httplib2、pycurl、requests等等都是和http相关的,用法大同小异,大家去各自官网看介绍就可以了,可见python的第三方库实在有点太多了啊。

你可能感兴趣的:(httplib和urllib2常用方法)