473,406 Members | 2,707 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,406 software developers and data experts.

File upload by urllib2

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 requests using
urllib2.urlopen (including cookie handling via ClientCookie) - which
means it would be extremely incovenient (including probably not
working without the cookie handling) to have to use httplib. It would
also mean duplicating the code...

I had thought of just adding the headers manually using add_header -
but for multipart data we need boundaries and the suchlike...... Has
anyone done this or know the right incantations to do it ?

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #1
3 8805
fu******@gmail.com (Michael Foord) writes:
Does anyone know how to do a 'file upload' using urllib2 ?

[...]

GIYF:

http://www.google.com/search?q=%22fi...+Feeling+Lucky
Example:

http://www.google.com/groups?threadm...sf%40pobox.com
John
Jul 18 '05 #2
fu******@gmail.com (Michael Foord) writes:
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.

[...]

Actually, more relevant than my previous post:

http://www.google.com/groups?threadm...son.monkey.org
Modifying the recipe that's pointed to from that thread very slightly
for urllib2 (untested!):

def post_multipart(url, fields, files):
content_type, body = encode_multipart_formdata(fields, files)
headers = {'Content-type': content_type,
'Content-length': str(len(body))}
req = urllib2.Request(selector, body, headers)
r = urllib2.urlopen(req)
return r.file.read()
If you get trouble, note my comment in that thread about bad servers.

John
Jul 18 '05 #3
jj*@pobox.com (John J. Lee) wrote in message news:<87************@pobox.com>...
fu******@gmail.com (Michael Foord) writes:
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.

[...]

Actually, more relevant than my previous post:

http://www.google.com/groups?threadm...son.monkey.org
Modifying the recipe that's pointed to from that thread very slightly
for urllib2 (untested!):

def post_multipart(url, fields, files):
content_type, body = encode_multipart_formdata(fields, files)
headers = {'Content-type': content_type,
'Content-length': str(len(body))}
req = urllib2.Request(selector, body, headers)
r = urllib2.urlopen(req)
return r.file.read()
If you get trouble, note my comment in that thread about bad servers.

John

Interesting, I'll give it a try.

Thanks

Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: O. Koch | last post by:
Until now, i know that ftplib doesn't support proxies and that i have to use urllib2. But i don't know how to use the urllib2 correct. I found some examples, but i don't understand them. Is...
4
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using...
2
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page....
1
by: Clark C. Evans | last post by:
Hello. I was wondering if anyone has built a module that works with urllib2 to upload file content via POST multipart/form-data. I'm aware of ASPN 146306, however, I need to use urllib2 beacuse...
1
by: Vasil Slavov | last post by:
I apologize for the long email. I hope somebody will have time to read it and give some suggestions. I am working on a school project written in Python (using mod_python) and I need to upload a...
9
by: Karsten.G.Weinert | last post by:
Hello, what is the simplest way to upload a file (or a long string) to a server using cgi/python? Since I want to upload the data programmatically, a form based solution is not good. I am not...
7
by: jld730 | last post by:
In the example below, how would you write the in-memory xmldoc to a text file? In my real situation, I send a string to a web service (code 2 below) which returns XML/KML that I want to save to a...
3
by: Alexnb | last post by:
Okay, I tried to follow that, and it is kinda hard. But since you obviously know what you are doing, where did you learn this? Or where can I learn this? Maric Michaud wrote:...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.