使用POST从Python脚本发送文件到PHP脚本

时间:2012-12-27 19:17:37

标签: php python image post

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

借助上述代码,我可以登录管理面板。在这里,我应该发布一些广告(这里我自动完成任务和涉及广告标题,广告设计和主要是一系列图像的字段(图像列表,我猜是更好的术语)。)

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":"abc@def.com",
        "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-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"

abc@def.com
-----------------------------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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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":"abc@def.com",
        "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)

------------------------------------------------