python发送post和get请求
2015-03-31 15:44
507 查看
python发送post和get请求
get请求:
使用get方式时,请求数据直接放在url中。
方法一、
方法二、
post请求:
使用post方式时,数据放在data或者body中,不能放在url中,放在url中将被忽略。
方法一、
方法二、
对python中json的使用不清楚,所以临时使用了urllib.urlencode(test_data)方法
get请求:
使用get方式时,请求数据直接放在url中。
方法一、
import urllib import urllib2 url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa" req = urllib2.Request(url) print req res_data = urllib2.urlopen(req) res = res_data.read() print res
方法二、
import httplib url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa" conn = httplib.HTTPConnection("192.168.81.16") conn.request(method="GET",url=url) response = conn.getresponse() res= response.read() print res
post请求:
使用post方式时,数据放在data或者body中,不能放在url中,放在url中将被忽略。
方法一、
import urllib import urllib2 test_data = {'ServiceCode':'aaaa','b':'bbbbb'} test_data_urlencode = urllib.urlencode(test_data) requrl = "http://192.168.81.16/cgi-bin/python_test/test.py" req = urllib2.Request(url = requrl,data =test_data_urlencode) print req res_data = urllib2.urlopen(req) res = res_data.read() print res
方法二、
import urllib import httplib test_data = {'ServiceCode':'aaaa','b':'bbbbb'} test_data_urlencode = urllib.urlencode(test_data) requrl = "http://192.168.81.16/cgi-bin/python_test/test.py" headerdata = {"Host":"192.168.81.16"} conn = httplib.HTTPConnection("192.168.81.16") conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata) response = conn.getresponse() res= response.read() print res
对python中json的使用不清楚,所以临时使用了urllib.urlencode(test_data)方法
相关文章推荐
- python中发送get或post请求
- python发送GET或POST请求以便干一些趣事
- Python 使用requests模块发送GET和POST请求的实现代码
- python发送GET或POST请求以便干一些趣事
- python用httplib模块发送get和post请求
- python学习笔记:使用requests库发送get与post请求
- python 发送 get post请求
- python发送GET或POST请求以便干一些趣事
- 【转】python发送GET或POST请求以便干一些趣事
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- python用httplib模块发送get和post请求
- python发送post和get请求
- python用httplib模块发送get和post请求
- python发送get/post/put/delete请求
- django 文件上传和菜单分级,mysql支持事务,F模块自增字段, python发送get,post请求
- Python 使用requests模块发送GET和POST请求的实现代码
- python用httplib模块发送get和post请求
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
- python发送GET或POST请求以便干一些趣事