python模块httplib的使用

时间:2023-03-09 22:34:15
python模块httplib的使用

GET:

 #lianxi-httplib.HTTPConnection.request-get.py

 import httplib

 class HttpRequestGETTest(object):
def __init__(self):
#self.body='{"UserName":"Admin","Password":"693aa8d0806c532115637809a863b1a3","sessionID":""}'
self.headers = {
"Referer": '192.168.1.1',
"Accept-Encoding": "gzip, deflate,sdch",
"Connection":"Keep-Alive"} def http_get(self):
conn=httplib.HTTPConnection(host='192.168.1.1', port=80, strict=False, timeout=30)
conn.request(method='GET',url='/cgi-bin/GetLoginStatus?sessionID=undefined', body=None, headers=self.headers)
a = conn.getresponse().read()
print a lianxi=HttpRequestGETTest()
lianxi.http_get()

POST:

 #lianxi-httplib.HTTPConnection.request-post.py

 import httplib

 class HttpRequestPOSTTest(object):
def __init__(self):
self.body='{"UserName":"Admin","Password":"693aa8d0806c532115637809a863b1a3","sessionID":""}'
self.headers = {
"Referer": '192.168.1.1',
"Accept-Encoding": "gzip, deflate,sdch",
"Connection":"Keep-Alive"} def http_post(self):
conn=httplib.HTTPConnection(host='192.168.1.1', port=80, strict=False, timeout=120)
conn.request(method='POST',url='/cgi-bin/Login', body=self.body, headers=self.headers)
self.session_id = conn.getresponse().read()
print self.session_id lianxi=HttpRequestPOSTTest()
lianxi.http_post()