Sending an HTTP GET Request from a Python Script


import httplib httpServ = \    httplib.HTTPConnection("testserver.net", 80) httpServ.connect() httpServ.request('GET', "/test.html") response = httpServ.getresponse() if response.status == httplib.OK:     printText (response.read()) httpServ.request('GET',     '/cgi_form.cgi?name=Brad&quote=Testing.') response = httpServ.getresponse() if response.status == httplib.OK:     printText (response.read())

Another important task when programming web services is to send GET requests directly to a web server from a Python script rather to than a web browser. This effectively allows you to write client-side applications without having to deal with the web browser.

The httplib module included with Python provides the classes and functions to connect to a web server, send a GET request, and handle the response.

First, create a server connection object by executing the httplib.HTTPConnection(address, port) function, which returns an HTTPServer object. Then, connect to the server by calling the connect() function of the HTTPServer object.

To send the GET request, call request(method [, url [, body [, headers). Specify GET as the method of the request, and then specify the location of the file as the url.

Note

In the sample code, we send a CGI script with parameters. Because the web server executed the CGI script, the response from the server will be the output of the CGI script, not the script itself.


After you have sent the request, get the servers' response using the getresponse() function of the HTTPServer object. The getresponse() function returns a response object that acts like a file object, allowing you to read the response using the read() request.

Note

You can check the status of the response by accessing the status attribute of the response object.


import httplib def printText(txt):     lines = txt.split('\n')     for line in lines:         print line.strip() #Connect to server httpServ = \    httplib.HTTPConnection("137.65.77.28", 80) httpServ.connect() #Send Get html request httpServ.request('GET', "/test.html") #Wait for response response = httpServ.getresponse() if response.status == httplib.OK:     print "Output from HTML request"     print "==========================="     printText (response.read()) #Send Get cgi request httpServ.request('GET', \     '/cgi_form.cgi?name=Brad&quote=Testing.') #Wait for response response = httpServ.getresponse() if response.status == httplib.OK:     print "Output from CGI request"     print "========================="     printText (response.read()) httpServ.close()


http_get.py

Output from HTML request =========================== <!DOCTYPE html> <html lang="en" xml:lang="en"> <head> <meta content="text/html; charset=utf-8"  http-equiv="content-type" /> <title>HTML Page</title> </head> <body> <h1>Test Link to CGI Script</h1> <a href="cgi_text.cgi">cgi_text.cgi</A></body> </html> Output from CGI request ========================= <title>CGI Form Response</title> <h2>Current Quote</h2><p> <b>Brad</b>: Testing.


Output from http_get.py code.




Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net