Python http upload script

Posted on May 15, 2009. Filed under: Python |

Python http upload script

 Httplib module and some useful methods

 Note   The httplib module has been renamed to http.client in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

class httplib.HTTPConnection(host[, port[, strict[, timeout]]])

 HTTPConnection.request(method, url[, body[, headers]])

 HTTPConnection.getresponse()

Note   Note that you must have read the whole response before you can send a new request to the server

 HTTPConnection.set_debuglevel(level)

Set the debugging level (the amount of debugging output printed). The default debug level is 0, meaning no debugging output is printed.

HTTPConnection.connect()

Connect to the server specified when the object was created.

HTTPConnection.close()

Close the connection to the server.

As an alternative to using the request() method described above, you can also send your request step by step, by using the four functions below.

HTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])

This should be the first call after the connection to the server has been made. It sends a line to the server consisting of the request string, the selector string, and the HTTP version (HTTP/1.1). To disable automatic sending of Host: or Accept-Encoding: headers (for example to accept additional content encodings), specify skip_host or skip_accept_encoding with non-False values.

Changed in version 2.4: skip_accept_encoding argument added.

HTTPConnection.putheader(header, argument[, ])

Send an RFC 822-style header to the server. It sends a line to the server consisting of the header, a colon and a space, and the first argument. If more arguments are given, continuation lines are sent, each consisting of a tab and an argument.

HTTPConnection.endheaders()

Send a blank line to the server, signalling the end of the headers.

HTTPConnection.send(data)

Send data to the server. This should be used directly only after the endheaders() method has been called and before getresponse() is called.

 

 Example of upload file by PUT

 import httplib

conn = httplib.HTTPConnection(“192.168.1.1”)

conn.set_debuglevel(10)

conn.putrequest(‘PUT’,’http://192.168.1.1/cgi-bin/upload.php’)

conn.putheader(“Content-Length”, “32”)

conn.endheaders()

conn.send(“Hello world, I am uploading 32 bytes, if the length is less than 32 bytes, the script will halt here, if more than 32 bytes, the upload.php will only read the first 32 bytes”)

resps = conn.getresponse()

data = resps.read()

print “response is the webpage which upload.php shows after PUT upload:”, data

 

References:

httplib — HTTP protocol client

http://docs.python.org/library/httplib.html

 urllib — Open arbitrary resources by URL

http://www.python.org/doc/2.6/library/urllib.html

Http client to POST using multipart/form-data

http://code.activestate.com/recipes/146306/

Big File Upload

http://webpython.codepoint.net/cgi_big_file_upload

httplib HTTPConnection request problem

http://bytes.com/groups/python/30065-httplib-httpconnection-request-problem

http://mail.python.org/pipermail/python-list/2004-July/272313.html

PyCURL interface – Uploading large binary files

http://curl.haxx.se/mail/archive-2004-02/0043.html

http://pycurl.sourceforge.net/

urlgrabber

http://linux.duke.edu/projects/urlgrabber/

http://linux.duke.edu/projects/urlgrabber/comparison.ptml

HTTP Upload — An Overview

http://www.chilkatsoft.com/p/p_200.asp

HTTP Upload using a Proxy Server

http://www.example-code.com/python/upload_proxy_server.asp

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: