pycurl post multipart/form-data

python post 复杂表单的方法

比如要提交如下例子中的表单:

POST /index.php HTTP/1.1
Host: www.test.com
User-Agent: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Referer: https://www.test.com
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Content-Length: 
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------f9a647dd330ea9b3


HTTP/1.1 100 Continue


--------------------------f9a647dd330ea9b3
Content-Disposition: form-data; name="user"

test
--------------------------f9a647dd330ea9b3
Content-Disposition: form-data; name="xxx"

xxxx1
--------------------------f9a647dd330ea9b3
Content-Disposition: form-data; name="ref"

http://www.test.com
--------------------------f9a647dd330ea9b3
Content-Disposition: form-data; name="passwd"

test_pwd
--------------------------f9a647dd330ea9b3--

方法1:

post_data=[("user","test"),("xxx","xxxx1"),("ref","http://www.test.com")]
post_data.append(("passwd","test_pwd")))
crl = pycurl.Curl()
url_post="http://www.test.com/index.php"
crl.setopt(pycurl.URL, url_post)  
crl.setopt(pycurl.POST, 1) 
crl.setopt(pycurl.HTTPPOST,post_data)
crl.setopt(pycurl.SSL_VERIFYPEER, 0)  
crl.setopt(pycurl.SSL_VERIFYHOST, 2)
crl.setopt(pycurl.REFERER,url_post)

strUserAgent="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
crl.setopt(pycurl.USERAGENT, strUserAgent)
crl.setopt(pycurl.MAXREDIRS,10)

crl.setopt(pycurl.FOLLOWLOCATION, 1)
strHeader=["Cache-Control: max-age=0","Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Upgrade-Insecure-Requests: 1","Accept-Encoding: gzip, deflate, sdch","Accept-Language: zh-CN,zh;q=0.8"]

crl.setopt(pycurl.HTTPHEADER,strHeader)

crl.setopt(pycurl.COOKIEJAR, "cookie1.txt")  
#crl.setopt(pycurl.COOKIEFILE, "cookie1.txt") #下次请求时用
crl.fp = StringIO.StringIO()
crl.setopt(crl.WRITEFUNCTION, crl.fp.write) 

crl.perform()
print crl.fp.getvalue()

参考:https://automatthias.wordpress.com/2013/04/07/http-put-with-multipartform-data-using-pycurl/


方法2

def test(debug_type, debug_msg):
    print "debug(%d): %s" % (debug_type, debug_msg)
def post():
	post_data="--------------------------f9a647dd330ea9b3\r\n\
	Content-Disposition: form-data; name=\"user\"\r\n\r\n\
	test\r\n\
	--------------------------f9a647dd330ea9b3\r\n\
	Content-Disposition: form-data; name=\"xxx\"\r\n\r\n\
	xxxx1\r\n\
	--------------------------f9a647dd330ea9b3\r\n\
	Content-Disposition: form-data; name=\"ref\"\r\n\r\n\
	http://www.test.com\r\n\
	--------------------------f9a647dd330ea9b3\
	Content-Disposition: form-data; name=\"passwd\"\r\n\r\n\
	test_pwd\r\n\
	--------------------------f9a647dd330ea9b3--"

	crl = pycurl.Curl()
	url_post="http://www.test.com/index.php"
	crl.setopt(pycurl.URL, url_post)  
	crl.setopt(pycurl.POST, 1) 
	crl.setopt(pycurl.POSTFIELDS,post_data)   //注意这行
	crl.setopt(pycurl.SSL_VERIFYPEER, 0)  
	crl.setopt(pycurl.SSL_VERIFYHOST, 2)
	crl.setopt(pycurl.REFERER,url_post)

	crl.setopt(pycurl.VERBOSE,1)  #调试
	crl.setopt(pycurl.DEBUGFUNCTION,test)

	strUserAgent="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
	crl.setopt(pycurl.USERAGENT, strUserAgent)
	crl.setopt(pycurl.MAXREDIRS,10)

	crl.setopt(pycurl.FOLLOWLOCATION, 1)
	#注意header的设置  Content-Type 必须对
	strHeader=["Content-Type:multipart/form-data; boundary=------------------------f9a647dd330ea9b3","Cache-Control: max-age=0","Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Upgrade-Insecure-Requests: 1","Accept-Encoding: gzip, deflate, sdch","Accept-Language: zh-CN,zh;q=0.8"]

	crl.setopt(pycurl.HTTPHEADER,strHeader)

	crl.setopt(pycurl.COOKIEJAR, "cookie1.txt")  
	#crl.setopt(pycurl.COOKIEFILE, "cookie1.txt") #下次请求时用
	crl.fp = StringIO.StringIO()
	crl.setopt(crl.WRITEFUNCTION, crl.fp.write) 

	crl.perform()
	print crl.fp.getvalue()

post()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值