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>
时间: 2024-10-01 05:06:11