51Testing软件测试论坛
标题:
python 发http请求,可以用来测试接口
[打印本页]
作者:
Jun_Li
时间:
2011-4-8 07:36
标题:
python 发http请求,可以用来测试接口
本帖最后由 Jun_Li 于 2011-6-9 23:13 编辑
python发送HTTP请求 今天用python写一个发送HTTP请求的功能,查了下文档,发现实现也就4、5行左右,不禁感叹啊,查了下原来找的java实现的,相比还是臃肿了很多。
所以,python的好处还是蛮多的,对于这些小的功能点相当适合。
附上官方的实例:
view sourceprint?01 Here is an example session that uses the GET method:
02
03 >>> import httplib
04 >>> conn = httplib.HTTPConnection("www.python.org")
05 >>> conn.request("GET", "/index.html")
06 >>> r1 = conn.getresponse()
07 >>> print r1.status, r1.reason
08 200 OK
09 >>> data1 = r1.read()
10 >>> conn.request("GET", "/parrot.spam")
11 >>> r2 = conn.getresponse()
12 >>> print r2.status, r2.reason
13 404 Not Found
14 >>> data2 = r2.read()
15 >>> conn.close()
16
17 Here is an example session that uses the HEAD method. Note that the HEAD method never returns any data.
18
19 >>> import httplib
20 >>> conn = httplib.HTTPConnection("www.python.org")
21 >>> conn.request("HEAD","/index.html")
22 >>> res = conn.getresponse()
23 >>> print res.status, res.reason
24 200 OK
[attach]73479[/attach]
25 >>> data = res.read()
26 >>> print len(data)
27 0
28 >>> data == ''
29 True
30
31 Here is an example session that shows how to POST requests:
32
33 >>> import httplib, urllib
34 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
35 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
36 ... "Accept": "text/plain"}
37 >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
38 >>> conn.request("POST", "/cgi-bin/query", params, headers)
39 >>> response = conn.getresponse()
40 >>> print response.status, response.reason
41 200 OK
42 >>> data = response.read()
43 >>> conn.close()
作者:
nimenhaomazm
时间:
2011-5-20 16:24
顶一下 谢谢分享
作者:
msnshow
时间:
2011-5-22 09:50
检查状态么,用curl更简单
作者:
echotaylor
时间:
2012-2-4 18:53
作者:
runfox545
时间:
2012-2-13 15:41
回复
1#
Jun_Li
高手啊
作者:
gffhhbqb
时间:
2012-3-26 13:20
我是来打酱油的
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br>
脸上毛孔粗怎么办
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2