File upload using httplib

A

alastair

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.
 
P

Peter Hansen

alastair said:
I'm attempting to test out some functionality of the Apache http
server.
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.

What you are doing is mostly fine. Why not try it at the
interactive interpreter prompt and experiment for yourself?
This is Python: there's no excuse for not playing. :)
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")

You don't use "headers" anywhere... note it should be a parameter
to conn.request().

At this point, though, you haven't really asked a question,
and your code should mostly work (barring syntax errors etc,
as I haven't tried it myself). Why not try it out and post
questions if you have trouble and can't solve it yourself?

-Peter
 
J

John J. Lee

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
...).

This is called 'HTTP file upload'. Use Google Groups to see lots of
posts about it in this newsgroup. Eg:

http://groups.google.com/groups?&[email protected]

If you haven't done already, install a sniffer or a proxy. Ethereal
works for me.

[...]
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")
[...]

As Peter said, you're failing to send the headers you think you are.

If you did send those headers, though, you would be asking the server
to believe you're POSTing application/x-www-form-urlencoded data
(stuff like foo=bar&spam=eggs), but sending it a Python file.


John
 
J

Jeff Shannon

alastair said:
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
...).

Keep in mind that, in order to send a file, you must use a content type
of multipart/form-data, rather than your current
application/x-www-form-urlencoded. Neither urllib nor httplib directly
support multipart/form-data, but (as someone else pointed out when I
asked about this very recently) there's a recipe for it in ActiveState's
Python Cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

Note that the recipe uses an older httplib.HTTP() interface rather than
the newer (and preferred) httplib.HTTPConnection() interface. However,
the only change necessary to use the new interface (beyond changing the
inital call) is to change the h.getreply() to h.getresponse(), and
handle the HTTPResponse object properly.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top