如何将数据发送到特定网址?

0 投票
2 回答
3487 浏览
提问于 2025-04-16 08:17

我想在一个特定的网址,比如 http://localhost:8000/postme/,发送一些数据。但是我还没有表单。所以,我想在没有表单的情况下测试一下。那么,我该怎么在没有表单的情况下向这个网址发送数据呢?我想在网页浏览器中看到响应结果。

2 个回答

1

你可以使用 curl 来实现这个功能:

curl -F "name=jonesy" http://localhost:8080/postme

或者你可以使用 twill 这个 Python 模块(它也有一个命令行界面):

twill-sh
jonesy$ twill-sh

-= Welcome to twill! =-

current page:  *empty page* 
>> go 'http://www.google.com' 
==> at http://www.google.com
current page: http://www.google.com
>> showforms

Form name=f (#1)
## ## __Name__________________ __Type___ __ID________ __Value__________________
1     hl                       hidden    (None)       en 
2     source                   hidden    (None)       hp 
3     ie                       hidden    (None)       ISO-8859-1 
4     q                        text      (None)        
5  1  btnG                     submit    (None)       Google Search 
6  2  btnI                     submit    (None)       I'm Feeling Lucky 

>> fv f q 'python'
current page: http://www.google.com
>> submit
Note: submit is using submit button: name="btnG", value="Google Search"

current page: http://www.google.com/search?hl=en&source=hp&ie=ISO-8859-1&q=python&btnG=Google+Search
>> save_html
(Using filename 'search')
current page: http://www.google.com/search?hl=en&source=hp&ie=ISO-8859-1&q=python&btnG=Google+Search
>> showlinks
Links:

0. Images ==> http://www.google.com/images?hl=en&q=python&um=1&ie=UTF-8&source=og&sa=N&tab=wi
1. Videos ==> http://www.google.com/search?hl=en&q=python&um=1&ie=UTF-8&tbo=u&tbs=vid:1&source=og&sa=N&tab=wv
2. Maps ==> http://maps.google.com/maps?hl=en&q=python&um=1&ie=UTF-8&sa=N&tab=wl
3. News ==> http://www.google.com/search?hl=en&q=python&um=1&ie=UTF-8&tbo=u&tbs=nws:1&source=og&sa=N&tab=wn
4. Shopping ==> http://www.google.com/search?hl=en&q=python&um=1&ie=UTF-8&tbo=u&tbs=shop:1&source=og&sa=N&tab=wf
5. Gmail ==> http://mail.google.com/mail/?hl=en&tab=wm

……链接可以一直延续下去,但你明白我的意思了。你可以像我这里那样在命令行中使用 twill,或者这些命令也可以在 Python 脚本中作为方法调用,来自 twill.commands 模块。

当然,你可以退出 twill 的命令行,打开 'search' 这个文件(在这个例子中),查看实际发送到浏览器的 HTML 内容。

5

你可以使用jQuery来进行Ajax的POST请求。想了解更多,可以查看这个页面,里面有相关的接口说明。

如果你使用Python的话,可以用httplib。想了解更多,可以查看这个页面,里面有相关的接口说明。下面是一个例子:

>>> import httplib, urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
200 OK
>>> data = response.read()
>>> conn.close()

撰写回答