python发送get和post请求

版权说明:本文系博主通过各种渠道学习整理发表或者全文转载自其他平台,作为学习笔记,不能保证所有知识点是完全正确以及表达无误,用于生产环境配置时请斟酌。如有错误或建议请联系。转载请注明出处,侵删联系:linuxops@foxmail.com。感谢各位!

get请求形式:

# -*- coding: utf-8 -*-            #这个可以允许代码中包含中文字符  
import urllib2  
import urllib  
data = {  
    "admin":"admin' and (ascii(substring(version(),1,1))<0) #",  
    "pass":"f",  
    "action":"login"  
}                                               #get请求的参数要分装成这样才能正常执行  
urldata=urllib.urlencode(data)  
print urldata  
url="http://ctf1.simplexue.com/basic/inject/index.php?"+urldata         #请求的域名加参数  
print url  
headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}     #发送的Http指令中包含http头部  
req = urllib2.Request(url,headers=headers)                                    #包装整个http请求  
resul=urllib2.urlopen(req).read()                                             #发送数据包并读取  
print resul.decode('gbk')                                                     #讲请求回来的数据包做编码处理,避免乱码

post请求形式:

# -*- coding: utf-8 -*-  

import urllib2  
import urllib  
data={}  
data["username"]="admin"  
data["password"]="pass"  
data["act"]="signin"  
url="http://admin.choumei.cn/index.php/Login/doSubmitLogin"  
post_data=urllib.urlencode(data)  
headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}  
req=urllib2.Request(url,headers=headers,data=post_data)  
resul=urllib2.urlopen(req)  
print resul.read()