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/Cookbook/Python/Recipe/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%3Dp10100397vj.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. -- http://mail.python.org/mailman/listinfo/python-list