468,532 Members | 1,663 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Uploading images to imageshack.us with Python

Hello,

I'm trying to upload images to http://imageshac.us via a Python script.
I have looked at the POST request with HTTPLiveHeaders Firefox
extension when I upload an image, but I can't figure what's wrong. (if
I disable the cookies in the browser, it still works, so it's not
that).

When I try to upload images with the script below, the server replies
with the following error:

<br />
<b>Warning</b>: Division by zero in <b>/home/image/www/index.php</b>
on line <b
270</b><br />


followed with the regular imageshack.us index.

Currently my code is the following one (I got it from
http://aspn.activestate.com/ASPN/Coo...ecipe/146306):

# ------------------------------------------
import urllib, httplib, mimetypes

def post_multipart(host, port, selector, fields, files):
"""
Post fields and files to an http host as multipart/form-data.
fields is a sequence of (name, value) elements for regular form
fields.
files is a sequence of (name, filename, value) elements for data to
be uploaded as files
Return the server's response page.
"""
content_type, body = encode_multipart_formdata(fields, files)

h = httplib.HTTP(host, port)

h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()

def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form
fields.
files is a sequence of (name, filename, value) elements for data to
be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '---------------------------13049614110900'

CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s";
filename="%s"' % (key, filename))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body

def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or
'application/octet-stream'

params = [('MAX_FILE_SIZE', '3145728'), ('refer',
'http://reg.imageshack.us/v_images.php')]
files = [('fileupload', 'b.jpg', open('b.jpg').read())]

print open('a.jpg').read()

print post_multipart('proxy-a.mains.nitech.ac.jp', 8080,
'http://imageshack.us/index.php', params, files)
# ------------------------------------------
Here is the HTTPLiveHeaders POST request:
# ------------------------------------------
http://imageshack.us/index.php

POST http://imageshack.us/index.php HTTP/1.1
Host: imageshack.us
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10)
Gecko/20050716 Firefox/1.0.6
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://imageshack.us/
Cookie: imgshck=4d590dbf69a8461ddff60153181b6c61;
img_gallery=e04663a565bc72348bfb2bdeec6d50a0%3Dp10 100397vj.jpg%3Dimg185;
PHPSESSID=1bd06f468149071bd87f7f8e90142cff
Content-Type: multipart/form-data;
boundary=---------------------------114782935826962
Content-Length: 90772
-----------------------------114782935826962
Content-Disposition: form-data; name="MAX_FILE_SIZE"

3145728
-----------------------------114782935826962
Content-Disposition: form-data; name="refer"
-----------------------------114782935826962
Content-Disposition: form-data; name="fileupload"; filename="a.jpg"
Content-Type: image/jpeg


HTTP/1.x 200 OK
Date: Mon, 22 Aug 2005 05:14:16 GMT
Server: Apache/2.0.54 (Unix)
X-Powered-By: PHP/4.4.0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
Pragma: no-cache
Connection: close
Content-Type: text/html; charset=ISO-8859-1
# ------------------------------------------
Any help would be appreciated.

Aug 22 '05 #1
3 3344
I forgot to add that I'm behind a proxy, but I think that is
irrelevant.

If you are not behind a proxy replace this line:

print post_multipart('proxy-a.mains.nitech.ac.jp', 8080,
'http://imageshack.us/index.php', params, files)

with

print post_multipart('imageshack.us', 80, '/index.php', params, files)

Aug 22 '05 #2
Ok, I solved it with the unvaluable help of a nice guy in the #python
channel.

It is a studid as it gets, replaced

files = [('fileupload', 'b.jpg', open('b.jpg').read())]

by

files = [('fileupload', 'b.jpg', open('b.jpg', 'rb').read())]

because binary files are not opened correctly in Windows XP unless you
specify that they are so.

Aug 22 '05 #3
I would love a script to upload images to Imageshack.us. Any chance you
can post the latest version or email it to me?

Thanks.

Ricardo Sanchez wrote:
I forgot to add that I'm behind a proxy, but I think that is
irrelevant.

If you are not behind a proxy replace this line:

print post_multipart('proxy-a.mains.nitech.ac.jp', 8080,
'http://imageshack.us/index.php', params, files)

with

print post_multipart('imageshack.us', 80, '/index.php', params, files)

Aug 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by laredotornado | last post: by
6 posts views Thread by Will Stuyvesant | last post: by
4 posts views Thread by Chris Dewin | last post: by
3 posts views Thread by Gavin | last post: by
pezholio
1 post views Thread by pezholio | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.