Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
import urllib,MultipartPostHandler,urllib2,cookielib
cookies = cookielib.CookieJar()
opener =     urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
login = urllib.urlencode(dict(admin_user='admin',admin_pass='****'))
o=opener.open('http://some_domain_name/admin/index.php',login)
print o.read()
### successfully logged-into the system ###################

With the help of above code I am able to log into admin panel. Here I am supposed to post some ads (Here I am automating the tasks and the fields covering ad-title, ad-desc, and mainly importantly series of images (list of images would be a better term I guess ;))

HTML Code snippet:

<form action="postad.php?cityid=15&subcatid=1" method="post" name="frmPost" enctype="multipart/form-data" 

 <!-- Some other code -->


<td><input name="showemail" type="radio" value="1" >&nbsp;</td>
<table class="postad" cellspacing="0" cellpadding="0" border="0" width="100%">

<tr>
    <td><b>Upload Pictures:</b><br>
    <span class="hint">Maximum filesize: 300KB</span><br>
                <input type="file" name="pic[]" size="69"><br>
        <img src="images/spacer.gif" height="2"><br>
                <input type="file" name="pic[]" size="69"><br>
        <img src="images/spacer.gif" height="2"><br>
                <input type="file" name="pic[]" size="69"><br>
        <img src="images/spacer.gif" height="2"><br>
                <input type="file" name="pic[]" size="69"><br>
        <img src="images/spacer.gif" height="2"><br>
                <input type="file" name="pic[]" size="69"><br>
        <img src="images/spacer.gif" height="2"><br>
            </td>
</tr>

</table>


<!-- some other code -->
<input name="do" type="hidden" id="do" value="post">
<button type="submit">Post Now</button>

So tackle this I am using this code, but Every time I am failing to attach images. Would you guys help me in this regard.

raw_params={"adtitle":"sample title",
        "area":"sample area",
        "addesc":"<p>sample post</p>",
        "price":"2000",
        "x[1]":"2012",
        "email":"[email protected]",
        "showemail":"2",
        "subcatid":"15",
    "do":"post",
        }

encoded_params=urllib.urlencode(raw_params)
target_page = 'http://some_domain_name/admin/postad.php?cityid=15&subcatid=15'
opener.open(target_page,encoded_params)

I forget to tell you one thing this is the log I got from LIVEHTTPHeader (mozilla-plugin)

Content-Disposition: form-data; name="adtitle"

sample title
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="area"

sample location
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="addesc"

<p>sample post</p>
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="price"

200
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="x[1]"

2012
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="email"

[email protected]
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="showemail"

2
-----------------------------20165274802361271281051822614
Content-Disposition: form-data; name="pic[]"; filename="3_d_flower.jpg"
Content-Type: image/jpeg

ÿØÿà

And please through some light on this: filename="3_d_flower.jpg

share|improve this question
 
I want to know how we can add images to the post request. –  USHA Dec 27 '12 at 19:19
add comment

2 Answers

I'd definitely recommend the requests library for this. It makes multi-part POSTs much simpler. It also handles cookies much more simply.

The login code would look like this in requests:

import requests
o = requests.get('http://some_domain_name/admin/index.php', 
                 data={admin_user: 'admin',
                       admin_pass: '****'})
print o.text
print o.cookies
share|improve this answer
 
I tried this, but its giving me the same login page. Its not working in my case. :( . Is there any possibility that without using any external libraries, I can add files. –  USHA Dec 27 '12 at 19:40
 
It's certainly possible, but getting requests installed and using it will be far far easier than trying to figure out all of the standard library magic. –  The UNIX Man Dec 27 '12 at 21:06
 
Also, I misread the original code, and now the code I've written shows how to send form data with the request instead of setting auth headers. –  The UNIX Man Dec 27 '12 at 21:07
 
Thanks man for your help, any comments on my solution. –  USHA Dec 28 '12 at 6:15
add comment
import urllib,MultipartPostHandler,urllib2,cookielib

cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
login = urllib.urlencode(dict(admin_user='admin',admin_pass='****'))
o=opener.open('http://some_domain_name.com/admin/index.php',login)
print o.read()
### logged-into the system ###################
### Now post the things ####################
raw_params={"adtitle":"sample title",
        "area":"sample area",
        "addesc":"<p>sample post</p>",
        "price":"2000",
        "x[1]":"2012",
        "email":"[email protected]",
        "showemail":"2",
    "pic[0]":open("indian_eye.jpg", "rb"),
    "pic[1]":open("nature.jpg", "rb"),
        "subcatid":"1",
    "do":"post",
        }




url="http://some_domain_name.com/admin/postad.php?cityid=15&subcatid=1"
opener.open(url, raw_params)

------------------------------------------------
share|improve this answer
 
No Fuss, just an easy answer. I Wonder How could I miss that. –  USHA Dec 28 '12 at 4:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.