File upload using httplib

alastair ally_burnett at yahoo.co.uk
Mon Jul 26 15:14:49 CEST 2004


Hi,

I'm attempting to test out some functionality of the Apache http
server. What I'd like to do is send a file to the server - eg. a text
file or binary file (I will be testing gzipped transfers eventually
...).

At the moment I can test out sending a set of parameters to the
server, and using mod_python, I have a python script which displays
these values.

I'm no expert when it comes to the http standard so I'm not sure if
using httplib is the right way to go about things. If anyone has any
advice on how to go about this, I'd appreciate it.

Here's the code I've used to initiate a 'POST' to the server (straight
out of the Python documentation):

import httplib, urllib

params = urllib.urlencode({'spam': 1, 'eggs': 2})

headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}

conn = httplib.HTTPConnection("127.0.0.1:8080")
conn.request("POST", "/test/test.py")
response = conn.getresponse()

print response.status, response.reason

data = response.read()
conn.close()


Cheers,

Alastair.



More information about the Python-list mailing list