1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #!/usr/bin/env python
#-*- encoding:UTF-8 -*-
#
import httplib, httplib2
import urllib, sys
rtr_ip = "192.168.0.1"
#
def check(var):
if not(var):
print "var= ", var, " ( fonction check(var) )\n"
exit(1)
#______________________________________________________________________________|
# page 1 = connection et login :
#
print "\n\nHello python script.py !\n"
connection = httplib.HTTPConnection(rtr_ip)
check(connection)
print "connection= ", connection # Ok jusque ici : <httplib.HTTPConnection instance at 0x97f1bec>
#
connection.set_debuglevel(2)
headers = {"Content-type": "application/x-www-form-urlencoded",
"Connection": "keep-alive",
"Accept": "text/plain"}
data = dict(loginUsername="admin", loginPassword="password")
connection.request("POST", "/goform/login", urllib.urlencode(data), headers) # ou
# connection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])
# connection.putheaders(header, argument[, ...])
# connection.endheaders()
# connection.send(data)
response = connection.getresponse()
check(response)
print "POST /goform/login ->", response.status, response.reason # 302 Redirect
print "response.getheaders()= ", response.getheaders() # [('connection', 'close'), ('content-type', 'text/html'),
# ('location', 'http://192.168.0.1:80/RgSwInfo.asp'), ('server', 'PS HTTP Server')]
print "response.getheader(\"Location\")= ", response.getheader("Location") # http://192.168.0.1:80/RgSwInfo.asp
print "response.read(100)= \"", response.read(100), "\"" # 2 blnks
print "Hello script.py page 1 = connection et login Ok\n"
#______________________________________________________________________________|
# page 1 = vérification du login :
connection.request("GET", "/login.asp")
response = connection.getresponse()
check(response)
print "GET /login.asp ->", response.status, response.reason # 200 OK
print "response.getheaders()= ", response.getheaders() # [('connection', 'close'), ('expires',
# 'Thu, 3 Oct 1968 12:00:00 GMT'), ('content-type', 'text/html'), ('content-length', '2616'), ('pragma', 'no-cache')]
print "response.getheader(\"Location\")= ", response.getheader("Location") # None
print "response.read(100)= \"", response.read(100), "\"" # <html> .(.. <html>)
print "Hello script.py page 1 = vérification du login\n"
#
#______________________________________________________________________________|
# page 2 = reinitialisation :
#
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
connection.request("GET", "/RgSecurity.asp")
response = connection.getresponse()
check(response)
print "response=", response # <httplib.HTTPResponse instance at 0x87340ec>
print "GET /RgSecurity.asp ->", response.status, response.reason #
print "response.getheader(\"Location\")= ", response.getheader("Location") #
#
data = dict(UserId="admin", Password="password", PasswordReEnter="password", OldPassword="password", RestoreFactoryYes="CHECKED")
connection.request("POST", "/goform/RgSecurity", urllib.urlencode(data), headers)
response = connection.getresponse()
check(response)
print "POST /goform/RgSecurity ->", response.status, response.reason # 200 OK
print "response.getheader(\"Location\")= ", response.getheader("Location") # None
#
connection.request("GET", "/RgSecurity.asp")
response = connection.getresponse() #
check(response)
print "response=", response # <httplib.HTTPResponse instance at 0x87340ec>
print "GET /RgSecurity.asp ->", response.status, response.reason # 200 OK
print "response.getheader(\"Location\")= ", response.getheader("Location") # None après le GET
print "response.getheaders()= ", response.getheaders() # [('connection', 'close'), ('expires', 'Thu, 3 Oct 1968 12:00:00 GMT'), ('content-type', 'text/html'), ('content-length', '6156'), ('pragma', 'no-cache')]
print "response.read(100)=", response.read(100) # <html> ... <html>
print "Hello script.py page 2 = reinitialisation\n"
#
#______________________________________________________________________________|
# page 3 = hote DMZ :
#
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
# connection.request("GET", "/RgDmzHost.asp")
connection.request("POST", "/goform/RgDmzHost", "DmzHostIP3=10", headers)
response = connection.getresponse()
check(response)
print "response=", response #
# print "GET /RgDmzHost.asp ->", response.status, response.reason # 200 OK
print "POST /goform/RgDmzHost ->", response.status, response.reason # 302 Redirect
print "response.getheader(\"Location\")= ", response.getheader("Location") #
print "response.getheaders()= ", response.getheaders() # [('connection', 'close'), ('content-type', 'text/html'),
# ('location', 'http://192.168.0.1:80/RgDmzHost.asp'), ('server', 'PS HTTP Server')]
print "response.read(100)= \"", response.read(100), "\"" # 2 blnks
print "Hello script.py page 3 = hote DMZ\n"
sys.exit()
#______________________________________________________________________________|
# Fin pour moi. |
Partager