469,609 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

File upload using httplib

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.
Jul 18 '05 #1
3 15072
alastair wrote:
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
Jul 18 '05 #2
al**********@yahoo.co.uk (alastair) writes:
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?&thr....supernews.com

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
Jul 18 '05 #3
alastair wrote:
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/Coo.../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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Milos Prudek | last post: by
3 posts views Thread by Michael Foord | last post: by
3 posts views Thread by Eric Pederson | last post: by
1 post views Thread by Oyvind Ostlund | last post: by
5 posts views Thread by scott | last post: by
6 posts views Thread by Haakon Riiser | last post: by
2 posts views Thread by wink | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.