@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府
本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法。分享给大家供大家参考。具体如下:
测试用CGI,名字为test.py,放在apache的cgi-bin目录下:
#!/usr/bin/python
import cgi
def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage()
if form.has_key("ServiceCode") and form["ServiceCode"].value != "":
print "<h1> Hello",form["ServiceCode"].value,"</h1>"
else:
print "<h1> Error! Please enter first name.</h1>"
main()
python发送post和get请求
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 =
Python实战:GET和POST方式发送HTTP请求及响应解析

本文介绍了Python通过GET和POST方式发送HTTP请求并接收响应的方法,包括get请求的两种实现、post请求的两种方式,以及涉及的HTTPConnection、request、getresponse等关键函数。同时讲解了HTTP头header的使用,强调了json数据在请求中的应用。最后,文章提及了学习Python和计算机基础知识的重要性。
最低0.47元/天 解锁文章
696

被折叠的 条评论
为什么被折叠?



