码迷,mamicode.com
首页 > 编程语言 > 详细

python 之 模拟GET/POST提交

时间:2015-07-13 15:27:48      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

以 POST/GET 方式向 http://127.0.0.1:8000/test/index 提交数据。

 1 # coding:utf-8
 2 import httplib
 3 import urllib
 4 
 5 class HttpClient(object):
 6     METHOD_POST = POST
 7     METHOD_GET  = GET
 8     REQUEST_HEADER = {Content-type: application/x-www-form-urlencoded,
 9                       Accept: text/plain}
10                    
11     def __init__(self, host, port=80):
12         self.host = host
13         self.port = port
14         
15     def doPost(self, body, url=‘‘, headers=REQUEST_HEADER):
16         return self._doRequest(self.METHOD_POST, urllib.urlencode(body), url, headers)
17         
18     def doGet(self, body=None, url=‘‘, headers={}):
19         return self._doRequest(self.METHOD_GET, body, url, headers)
20         
21     def _doRequest(self, method, body, url, headers):
22         try:
23             conn = httplib.HTTPConnection(self.host, self.port)
24             conn.request(method, url, body, headers)
25             response = conn.getresponse()
26             result = response.read()
27             conn.close()
28         except:
29             raise
30         finally:
31             conn.close()
32         return response.status, result
33             
34 def send():
35     client = HttpClient(127.0.0.1, 8000)
36     params = {message: test,
37               username : testUser,
38               password: testPwd
39               }
40     print (client.doPost(params, /test/index))    # post方式    
41     print (client.doGet(params, /test/index))     # get方式
42     
43 if __name__ == __main__:
44     send()

 

python 之 模拟GET/POST提交

标签:

原文地址:http://www.cnblogs.com/liuq/p/4642913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!