472,776 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

4
by: laredotornado | last post by:
Hi, I'm using PHP 4 and I am submitting some images in a form with <form name=addProductForm enctype="multipart/form-data" method=post action="add_product_response.php"> <input type=file...
6
by: Will Stuyvesant | last post by:
I am uploading a .zip file to a Python CGI program, using a form on a HTML page with <input name="yourfile" type="file">... In the Python CGI program I do: import cgi fStorage =...
4
by: Chris Dewin | last post by:
How do I go about writing a cgi script, that will enable the client to upload things to a directory on my website? I would also like to write a script that enables the client to delete items in...
3
by: Gavin | last post by:
I need some help, I need the code to allow people that visit my website to be able to upload pictures to a file on my web server. I have been able to get close, but not quite there yet. Set fso...
1
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
1
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I...
1
by: thulaseeram | last post by:
I am using iframe to store uploaded images, it is uploading fine in IE but it is not happening in firefox means first time it is uploading image if i try to upload second image it is not calling even...
14
w33nie
by: w33nie | last post by:
What I'm trying to do here, is upload a video to the ../video/ folder, and up to 5 images to the ../images/ folder. As well as the database information like title, content and each file's file...
1
pezholio
by: pezholio | last post by:
Hi, It seems that every time I put together a new script to upload a file I always have problems, here's the latest one: I've got a form with two file input fields, when I submit the form,...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.