【问题标题】:how to fill a form inside cherrypy web service using curl如何使用 curl 在cherrypy Web服务中填写表单
【发布时间】:2014-06-23 14:57:50
【问题描述】:

我有一个运行良好的 Cerrypy 服务,它包含一个只有 textarea 的表单,输入文本在同一个服务中由 RE 转换并在屏幕中返回转换后的文本,问题是现在我需要要从命令行发送数据,我已经按照此处找到的文档尝试使用 curl:http://curl.haxx.se/docs/httpscripting.html#Forms_explained,但我无法使其工作:

表格是这样的:

<form method="POST" action="NMT">
<div><table width=100% bgcolor="D2CAC1"><tr><td>
<textarea class="richtextbox" name="contents" style="width:100%;height:300px">
</textarea>
</td></tr></table></div>
<a title="unificar"><input type="submit" value=" trapümün " /></a>
</form>

功能是这样的:

 #define form function
    def NMT(self, contents=None):
            if not contents:
                    return """<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                                    <title>NMT - Norwirin Mapudungun Trap&uuml;mfe</title></head>
                                    <body><font face=arial>Inserte el texto para unificar<br><a href=.>Inténtelo de nuevo</a></fon>
                                    </body></html>"""

我试过了:

curl --data-urlencode "contents=zomo" http://www.chandia.net:8080
curl --data "contents=zomo" http://www.chandia.net:8080
curl --data "contents=zomo&submit=%20trapümün%20" http://89.140.140.36:8080

我也将 POST 更改为 GET 来试试这个:

curl "http://www.chandia.net:8080?contents=zomo"
curl "http://www.chandia.net:8080?contents=zomo&trapümün"
curl "http://www.chandia.net:8080?contents=zomo&%20trapümün%20"
curl "http://www.chandia.net:8080?contents=zomo&submit=%20trapümün%20"

结果它有点变化但总是说:

<body>
    <h2>400 Bad Request</h2>
    <p>Unexpected body parameters: contents</p>
    <pre id="traceback">Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 40, in __call__
raise sys.exc_info()[1]
HTTPError: (400, 'Unexpected body parameters: contents')

任何建议,或者也许有其他方法可以做到......?

提前致谢

【问题讨论】:

    标签: php forms curl python-3.x cherrypy


    【解决方案1】:

    您的处理程序定义必须包含以下内容...

    def NMT(self, contents)
    

    def NMT(self, *args, **kwargs)
    

    编辑: 您需要在您的 url 中指定处理程序。

    curl --data-urlencode "contents=zomo" http://www.chandia.net:8080/NMT
    

    希望这会有所帮助!

    【讨论】:

    • 我添加了我编辑原始问题的内容,这样正确吗?
    • 实际上,如果您看到我尝试过的东西,您建议的就是其中之一: curl --data-urlencode "contents=zomo" chandia.net:8080
    • 您问题中的 curl 请求并未显示您将转到 /NMT 处理程序。默认情况下,去往 / 的请求将由 def 索引处理。当前不接受变量内容。所以你看到的错误是准确的。
    • 对不起,我没听懂你在说什么,那我该怎么办?
    • 将你的url在curl命令中的路径改为... "chandia.net:8080/NMT"
    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多