Python 模擬網頁request 取得Post 或 Get的結果

我們可以利用程式的方式來模擬post傳送參數到某一個網頁,然後看回傳的結果為何.

假設有二個參數,傳送的值為param1= 'a1',param2= '123'
網址為XXX.XXX.XXX.XXX



import httplib, urllib
params = urllib.urlencode({'@param1': 'a1', '@param2': '123'})
headers = {"Content-type": "application/x-www-form-urlencoded",
            "Accept": "text/plain"}
conn = httplib.HTTPConnection("XXX.XXX.XXX.XXX")
conn.request("POST", "", params, headers)
response = conn.getresponse()
print response.status, response.reason
conn.close()