python+httplib实现HTTP POST提交数据,使用Basic Authentication(基本验证)做登陆验证
-
-
- import base64
- import urllib
- import httplib
-
- os = 'xp'
- eth0_ip = '192.168.1.1'
-
- params = urllib.urlencode({"os": os, "eth0_ip": eth0_ip})
- auth = base64.b64encode('cleartext username'+ ':'+ 'cleartext passwords')
- headers = {"Authorization": "Basic "+ auth}
- conn = httplib.HTTPConnection("10.10.10.10")
- conn.request("POST","/my/cgi-bin/test.sh", params, headers)
- response = conn.getresponse()
-
- print response.read().strip()
urllib.urlencode此处起到的作用仅是将要post的数据做成标准的格式,也可以手动敲,即params = 'os=xpð0_ip=192.168.1.1',这和params = urllib.urlencode({"os": os, "eth0_ip": eth0_ip}) 的结果一致。