2012-12-27 127 views
0
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 ################### 

藉助上述代碼,我可以登錄到管理面板。在這裏,我應該發佈一些廣告(在這裏,我自動化的任務和領域包括廣告標題,廣告desc,主要是一系列圖像(圖像列表將是一個更好的術語,我猜;))使用POST從Python腳本發送文件到PHP腳本

HTML代碼片段:

<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> 

所以解決這個我用這個代碼,但每一次,我不能附加圖像。在這方面你們能幫助我嗎?

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) 

我忘了告訴你一兩件事,這是我從LIVEHTTPHeader(Mozilla的插件)

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 

ÿØÿà 

得到了日誌,並請通過一些光在這: 文件名=「3_d_flower.jpg

+0

我想知道我們如何能夠將圖像添加到POST請求。 – USHA

回答

0

我肯定會推薦的requests庫這一點。它使多部分的POST much simpler,也handles cookies更加簡單。

登錄的代碼看起來像這樣的請求:

import requests 
o = requests.get('http://some_domain_name/admin/index.php', 
       data={admin_user: 'admin', 
         admin_pass: '****'}) 
print o.text 
print o.cookies 
+0

我試過了,但是它給了我相同的登錄頁面。它不適合我的情況。 :(有沒有任何可能,如果沒有使用任何外部庫,我可以添加文件 – USHA

+0

這當然是可能的,但獲取請求的安裝和使用將遠遠比試圖找出所有標準庫的魔術容易得多 – Misandrist

+0

也,我誤解了原始代碼,現在我編寫的代碼顯示瞭如何使用請求發送表單數據,而不是設置auth頭文件 – Misandrist

0
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) 

------------------------------------------------ 
+0

No Fuss,just a easy我不知道我怎麼會錯過這個。 – USHA