I am trying to send a HTTP post request in a PHP page. I gave a try on both Java and Python (this was the first time I used Python) and I can say that both of them worked almost fine for me. Actually, they worked fine only for my test PHP page. For some reason, neither of them worked for my target PHP page.
In my opinion, two reasons that may cause the problem of unsuccessful post request could be:
- a kind of redirection may happen
- the server wants a proper cookie
Next, you will find the HTML code of the target page and the Python code that should be working.
HTML:
<form id="m713a0moduleform_2" method="post" action="http://www.X.Y/index.php?page=login" class="cms_form">
<input type="hidden" name="mact" value="FrontEndUsers,m713a0,do_login,1" />
<input type="hidden" name="m713a0returnid" value="794" />
<input type="hidden" name="page" value="794" />
<input type="hidden" name="m713a0form" value="login" />
<input type="text" class="cms_textfield" name="m713a0feu_input_username" id="m713a0feu_input_username" value="" size="10" maxlength="40" />
<input type="password" class="cms_password" name="m713a0feu_input_password" value="" size="10" maxlength="10" />
<input class="cms_submit" name="m713a0feu_btn_login" id="m713a0feu_btn_login" value="Sign in" type="submit" class="signin_button" />
</form>
Python:
import urllib
params = urllib.urlenco开发者_JAVA百科de({"mact":"FrontEndUsers,m713a0,do_login,1","m713a0returnid":"18","page":"18","m713a0form":"login","m713a0feu_input_username":"Z","m713a0feu_input_password":"W","m713a0feu_btn_login":"Sign in"})
f = urllib.urlopen("http://www.X.Y/index.php?page=login", params)
print f.read()
I receive the following error. Any ideas?
Traceback (most recent call last): File "/X/Y/Z/NewPythonProject2/src/newpythonproject2.py", line 34, in from paste.proxy import TransparentProxy ImportError: No module named paste
I use paste.proxy.TransparentProxy and webob.Request ...
You need to install the libraries
easy_install webob webtest paste
or
pip install webob webtest paste
then in a script...
from paste.proxy import TransparentProxy
from webob import Request
proxy_app = TransparentProxy()
request = Request.blank("http://pathto/your_file.php", POST=dict(field_a=value_a,field_b=value_b))
response = request.get_response(proxy_app)
if you need to do anything fancier like maintain cookies across requests like passing cookies back you could use WebTest and you would only a few changes
from paste.proxy import TransparentProxy
from webtest import TestApp
app = TestApp(TransparentProxy())
app.post("http://pathto/your_file.php", dict(field_a=value_a,field_b=value_b))
精彩评论