how to post data to particular url?
I want to post some data
in the开发者_如何学编程 particular url say http://localhost:8000/postme/
. But I have know form yet. So, I want to test it without any form. So, How can I post data to the url without any form? I want to see the response to the web browser.
You can do an Ajax post using jQuery. See this page for the API.
Or for Python, you can use httplib
. See this page for the API. Here's an example:
>>> 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()
You can use curl
for this:
curl -F "name=jonesy" http://localhost:8080/postme
Or you can use the twill python module (which also has a shell interface):
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
....the links go on forever, but you get the point. You can use twill in the shell like I did here, or those commands can be called as methods in the twill.commands module in a python script.
Of course, you can leave the twill shell and open the file 'search' (in this example) to see the actual html that went to the browser.
精彩评论