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

get/post请求和 python 发起请求(4)

时间:2015-09-16 12:11:57      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

get 请求:

cgi-bin/hello_get.py
#!C:/python27/python.exe
# -*- coding: UTF-8 -*-


import cgi, cgitb 

form = cgi.FieldStorage() 


first_name = form.getvalue(first_name)
last_name  = form.getvalue(last_name)

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

python的get 请求

>>> import urllib2
>>> req=http://localhost/cgi-bin/hello_get.py?first_name=a&last_name=b
>>> response=urllib2.Request(req)  #response.get_method()可以查看使用何种请求方式
>>> page=urllib2.urlopen(response)
>>> print page.read()

<html>
<head>
<title>Hello - Second CGI Program</title>
</head>
<body>
<h2>Hello a b</h2>
</body>
</html>

 

post请求对应的py和get 一样

对应的post请求

>>> import urllib2,urllib
>>> url=http://localhost/cgi-bin/hello_get.py
>>> values={first_name:A,last_name:B}
>>> data=urllib.urlencode(values)
>>> req=urllib2.Request(url,data)
>>> response=urllib2.urlopen(req)
>>> print response.read()

<html>
<head>
<title>Hello - Second CGI Program</title>
</head>
<body>
<h2>Hello A B</h2>
</body>
</html>

 

get/post请求和 python 发起请求(4)

标签:

原文地址:http://www.cnblogs.com/Citizen/p/4812777.html

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