dongyun6003 2011-04-13 23:14
浏览 596

在Python中模拟HTTP Post请求

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:

  1. a kind of redirection may happen
  2. 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.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
  • 写回答

1条回答

  • dpbf62565 2011-04-14 01:06
    关注

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘