473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 15422
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Milos Prudek | last post by:
How can I set httplib timeout for httplib.request() ? httplib.request timeouts after 3:10 with "socket" timeout error message. I found socket.settimeout() and I believe there is a way to add...
3
by: Michael Foord | last post by:
Does anyone know how to do a 'file upload' using urllib2 ? There is an example of doing file upload using httplib - on activestate python cookbook. I have code that uses a request object to make...
3
by: Eric Pederson | last post by:
I didn't come across any illuminating discussion via Google, thus my question here (though it may be a neophyte question.) I am interested in the workings under the hood of Python's access of...
1
by: Oyvind Ostlund | last post by:
I am trying to read a file online, and was just testing a bit. This is what I tried. -------------------------------------- import sys, httplib showlines = 6 try: servername, filename =...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
5
by: scott | last post by:
Hello, From a shell script, I have used /usr/bin/curl to access a web site and pass a cookie (as required by the site). But, I can't seem to accomplish this task with Python. I would like to...
6
by: Haakon Riiser | last post by:
After a long debugging session while scripting my webmail, I believe I have traced the problem to the way httplib sends POST requests. I have compared tcpdump listings from Python 2.4.3 and...
2
by: wink | last post by:
Hello, I would like to know what would be considered the most Pythonic way of handling errors when dealing with files, solutions that seem reasonable using 2.5: ------- try: f =...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.