2010-12-09 46 views
0

我想post some data在特定的網址中說http://localhost:8000/postme/。但我已經知道形式了。所以,我想測試它沒有任何形式。那麼,如何將數據發佈到網址中而沒有任何形式?我想查看對Web瀏覽器的響應。如何發佈數據到特定的網址?

回答

5

你可以使用jQuery做一個Ajax文章。請參閱this頁面瞭解API。對於Python,可以使用httplib。請參閱this頁面。這裏有一個例子:

>>> 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() 
+0

但這不會顯示對Web瀏覽器的響應。我想查看對Web瀏覽器的響應。 – 2010-12-09 10:41:05

1

您可以使用curl這個:

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

或者你也可以使用斜紋Python模塊(其中也有一個shell界面):

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 

。 ......這些鏈接永遠持續下去,但你明白了。你可以像在這裏一樣在shell中使用斜紋,或者可以在Python腳本的twill.commands模塊中將這些命令調用爲方法。

當然,你可以離開斜紋殼並打開'搜索'文件(在這個例子中)來查看到瀏覽器的實際html。

相關問題