473,660 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_multipar t_formdata(fiel ds, files)

h = httplib.HTTP(ho st, port)

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

def encode_multipar t_formdata(fiel ds, 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('Conte nt-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Conte nt-Disposition: form-data; name="%s";
filename="%s"' % (key, filename))
L.append('Conte nt-Type: %s' % get_content_typ e(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_typ e(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').r ead())]

print open('a.jpg').r ead()

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,ima ge/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=4d590db f69a8461ddff601 53181b6c61;
img_gallery=e04 663a565bc72348b fb2bdeec6d50a0% 3Dp10100397vj.j pg%3Dimg185;
PHPSESSID=1bd06 f468149071bd87f 7f8e90142cff
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="fileuploa d"; 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 3726
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').r ead())]

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
2709
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 name='prodImg' id='prodImg'> However, sometimes, the images fail to upload, even though the form submits fine. When I try to print out the values from the $_FILES array, I get
6
3834
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 = cgi.FieldStorage() zipdata = fStorage.value
4
2657
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 that directory if they want. Given that it's unix server, how do I go about ensuring the files are written to that dir with the right permissions?
3
2303
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 = createobject("scripting.filesystemobject") Set act = fso.CreateTextFile(server.mappath("test.jpg"), true) act.WriteLine act.Close The above code will create the file but will not write any data in it. Below
1
2157
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
1
4409
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 would give it a try. First of all, I am using Visual Studio 2005 to write my program. I am using C# .NET as the language. I am running Windows XP Professional with all service packs and updates applied. Now, I have been trying to write a...
1
2067
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 java script function and not uploading the image in firefox. I am using this code in the button onclick event "attach_frame.upload();" here attach_frame is the iframe name and id to which the uploaded image should add. please can any one help me to...
14
4469
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 information. But I really have no idea how to do this. The whole move_uploaded_file part is obviously wrong, but I'm not sure where to go from here to achieve my ends. $videoUploadDir = '../video/'; $imageUploadDir = '../images/'; ...
1
2121
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, everything goes OK, but the files don't upload, the permissions on the folders are fine and dandy (Both 777). Here's my code: if(is_uploaded_file($_FILES)){ $target_path = "/home/default/ratemyplace.org.uk/user/htdocs/newsite/images/premimages/";
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.