简体   繁体   English

在Python中模拟HTTP发布请求

[英]Emulating a HTTP Post Request in Python

I am trying to send a HTTP post request in a PHP page. 我正在尝试在PHP页面中发送HTTP发布请求。 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. 我尝试了Java和Python(这是我第一次使用Python),可以说它们对我来说都差不多。 Actually, they worked fine only for my test PHP page. 实际上,它们仅在我的测试PHP页面上运行良好。 For some reason, neither of them worked for my target PHP page. 由于某种原因,它们都不适合我的目标PHP页面。

In my opinion, two reasons that may cause the problem of unsuccessful post request could be: 我认为,可能导致邮寄请求失败的两个原因可能是:

  1. a kind of redirection may happen 可能会发生某种重定向
  2. the server wants a proper cookie 服务器需要一个适当的cookie

Next, you will find the HTML code of the target page and the Python code that should be working. 接下来,您将找到目标页面的HTML代码和应该运行的Python代码。

HTML: 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.urlencode({"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 ... 我使用paste.proxy.TransparentProxywebob.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 如果您需要做一些更奇特的事情,例如在请求之间维护cookie(例如将cookie传回),则可以使用WebTest ,而只需进行一些更改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM