The example in this article describes how Python sends HTTP requests and receives HTTP responses by Get,post. Share to everyone for your reference. Specifically as follows:
Test with CGI, named test.py, placed in the Apache Cgi-bin directory:
?
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/python import CGI def main (): print "content-type:text/htmln" form = cgi. Fieldstorage () If Form.has_key ("Servicecode") and form["Servicecode"].value!= "": Print " |
Python sends Post and GET requests
GET Request:
When you use the Get method, the request data is placed directly in the URL.
Method One,
?
1 2 3 4 5 6 7 8 |
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 |
Method Two,
?
1 2 3 4 5 6 7 |
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 request:
When you use the Post method, the data is placed in the information or body, cannot be placed in the URL, and is ignored in the URL.
Method One,
?
1 2 3 4 5 6 7 8 9 10 |
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 |
Method Two,
?
1 2 3 4 5 6 7 8 9 10 11 |
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 |
The use of JSON in Python is not clear, so the Urllib.urlencode (Test_data) method is used temporarily;
The difference of module Urllib,urllib2,httplib
Httplib implements the HTTP and HTTPS client protocols, but in Python, modules Urllib and URLLIB2 have a higher layer of encapsulation for httplib.
Describes the functions used in the following example:
1. httpconnection function
Httplib. Httpconnection (Host[,port[,stict[,timeout]])
This is a constructor that represents an interaction with the server at a time, that is, the request/response
Host identifies the server host (server IP or domain name)
Port defaults are 80
The strict mode is false, indicating that the state row returned by the server cannot be resolved, and whether the Badstatusline exception is thrown
For example:
conn = Httplib. Httpconnection ("192.168.81.16", 80) establishes a link with the server.
2, Httpconnection.request (Method,url[,body[,header]]) function
This is to send a request to the server
method is requested, usually by post or get,
For example:
Method= "POST" or method= "get"
URL requested resource, requested resource (page or CGI, we have CGI here)
For example:
Url= "http://192.168.81.16/cgi-bin/python_test/test.py" request CGI
Or
Url= "http://192.168.81.16/python_test/test.html" Request page
The body needs to submit data to the server, either in JSON or in the format above, JSON needs to call the JSON module
Headers requested HTTP Header headerdata = {"Host": "192.168.81.16"}
For example:
?
1 2 3 4 5 6 |
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) |
Conn after use, should be closed, conn.close ()
3, Httpconnection.getresponse () function
This is to get the HTTP response, and the returned object is an instance of HttpResponse.
4, HttpResponse Introduction:
The HttpResponse properties are as follows:
Read ([Amt]) Gets the response message body, which reads the specified byte of data from the response stream and reads all the data when unspecified;
GetHeader (Name[,default]) Gets the response Header,name is the header domain name, and in the absence of a header domain name, default is used to specify the return value
Getheaders () to get header in the form of a list
For example:
?
1 2 3 4 5 |
Date=response.getheader (' Date '); Print Date resheader= ' resheader=response.getheaders (); Print Resheader |
Response header information in column form:
?
1 2 3 |
[(' Content-length ', ' 295 '), (' accept-ranges ', ' bytes '), (' Server ', ' Apache '), (' last-modified ', ' Sat, Mar 2012 10:07:0 2 GMT '), (' Connection ', ' close '), (' ETag ', ' "E8744-127-4bc871e4fdd80"), (' Date ', ' Mon, Sep 10:01:47 GMT '), (' Cont Ent-type ', ' text/html ')] date=response.getheader (' Date '); Print Date |
Remove the value of date from the response header.
I hope this article will help you with your Python programming.