Difference between revisions of "Auth.py"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: =Script= <pre> #!/usr/bin/env python import sys import httplib import urllib #--------------------------------------------------------------------- class Error: pass def __init_...) |
PeterHarding (talk | contribs) |
||
Line 190: | Line 190: | ||
[[Category:Python]] | [[Category:Python]] | ||
[[Category:httplib]] | |||
[[Category:Examples]] | [[Category:Examples]] |
Revision as of 14:00, 7 August 2008
Script
#!/usr/bin/env python import sys import httplib import urllib #--------------------------------------------------------------------- class Error: pass def __init__(self, url, errcode, errmsg, headers): self.url = url self.errcode = errcode self.errmsg = errmsg self.headers = headers def __repr__(self): return ( "<Error for %s: %s %s>" % (self.url, self.errcode, self.errmsg) ) #--------------------------------------------------------------------- class Server: pass def __init__(self, host): self.Host = host def fetch(self, path): http = httplib.HTTP(self.Host) # write stuff... errcode, errmsg, headers = http.getreply() if errcode != 200: raise Error(errcode, errmsg, headers) file = http.getfile() return file.read() #--------------------------------------------------------------------- headers = { 'Referer' : 'http://gondor.toll.com.au/toll/authentication/Login.jsp', 'Connection' : 'Keep-Alive', 'User-Agent' : 'Mozilla/4.05 [fr] (X11; I; SunOS 5.7 sun4m)', 'Host' : 'gondor.toll.com.au', 'Accept' : 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*', 'Accept-Language' : 'en', 'Accept-Charset' : 'iso-8859-1,*,utf-8' } # "Content-type" : "application/x-www-form-urlencoded", # "Accept" : "text/plain" params = urllib.urlencode({ 'redir' : '/servlets/glog.webserver.util.FrameGC3Servlet', 'namespace' : 'GC3', 'username' : 'MTS.MENZIESK', 'password' : 'tess11', 'Log In.x' : '7', 'Log In.y' : '7' }) # print params #--------------------------------------------------------------------- data = 'redir=%2Fservlets%2Fglog.webserver.util.FrameGC3Servlet&namespace=GC3&username=MTS.XX&password=tess11&Log+In.x=7&Log+In.y=7' #data = 'redir=%2Fservlets%2Fglog.webserver.util.FrameGC3Servlet&namespace=GC3&username=MTS.MENZIESK&userpassword=tess11' conn = httplib.HTTPConnection("gondor.toll.com.au") conn.putrequest('POST', '/toll/authentication/Login.jsp') conn.putheader('Referer', 'http://gondor.toll.com.au/toll/authentication/Login.jsp') conn.putheader('Connection', 'Keep-Alive') conn.putheader('User-Agent', 'Mozilla/4.05 [fr] (X11; I; SunOS 5.7 sun4m)') conn.putheader('Host', 'gondor.toll.com.au') conn.putheader('Accept', 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*') conn.putheader('Accept-Language', 'en') conn.putheader('Accept-Charset', 'iso-8859-1,*,utf-8') conn.putheader('Cookie', 'JSESSIONID=4ED48EAEC9B0CECEB485DF1787430D64') conn.putheader('Content-type', 'application/x-www-form-urlencoded') conn.putheader('Content-length', '%d' % len(data)) conn.endheaders() conn.send(data) print len(data) # errcode, errmsg, headers = conn.getreply() # if errcode != 200: # raise Error(errcode, errmsg, headers) # file = http.getfile() # return file.read() response = conn.getresponse() data = response.read() print "Status %s Reason \"%s\"" % (response.status, response.reason) print "Data \"%s\"" % data # conn.close() #--------------------------------------------------------------------- # conn = httplib.HTTPConnection("gondor.toll.com.au") conn.putrequest("GET", "/servlets/glog.webserver.servlet.umt.Login?redir=/servlets/glog.webserver.util.FrameGC3Servlet&namespace=GC3&username=MTS.MENZIESK&userpassword=tess11") conn.putheader('Referer', 'http://gondor.toll.com.au/toll/authentication/Login.jsp') conn.putheader('Connection', 'Keep-Alive') conn.putheader('User-Agent', 'Mozilla/4.05 [fr] (X11; I; SunOS 5.7 sun4m)') conn.putheader('Host', 'gondor.toll.com.au') conn.putheader('Accept', 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*') conn.putheader('Accept-Language', 'en') conn.putheader('Accept-Charset', 'iso-8859-1,*,utf-8') conn.endheaders() response = conn.getresponse() data = response.read() print "Status %s Reason \"%s\"" % (response.status, response.reason) print "Data \"%s\"" % data conn.close() #--------------------------------------------------------------------- conn = httplib.HTTPConnection("gondor.toll.com.au") conn.putrequest("GET", "/servlets/glog.webserver.servlet.umt.Login?redir=/servlets/glog.webserver.util.FrameGC3Servlet&namespace=Default&username=MTS.MENZIESK&userpassword=tess11") conn.putheader('Referer', 'http://gondor.toll.com.au/toll/authentication/Login.jsp') conn.putheader('Connection', 'Keep-Alive') conn.putheader('User-Agent', 'Mozilla/4.05 [fr] (X11; I; SunOS 5.7 sun4m)') conn.putheader('Host', 'gondor.toll.com.au') conn.putheader('Accept', 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*') conn.putheader('Accept-Language', 'en') conn.putheader('Accept-Charset', 'iso-8859-1,*,utf-8') conn.endheaders() response = conn.getresponse() data = response.read() print "Status %s Reason \"%s\"" % (response.status, response.reason) print "Data \"%s\"" % data conn.close() #--------------------------------------------------------------------- conn = httplib.HTTPConnection("gondor.toll.com.au") conn.putrequest("GET", "/servlets/glog.webserver.servlet.umt.Login?redir=/servlets/glog.webserver.util.FrameGC3Servlet&namespace=Default&username=MTS.MENZIESK&userpassword=tess11") conn.putheader('Referer', 'http://gondor.toll.com.au/toll/authentication/Login.jsp') conn.putheader('Connection', 'Keep-Alive') conn.putheader('User-Agent', 'Mozilla/4.05 [fr] (X11; I; SunOS 5.7 sun4m)') conn.putheader('Host', 'gondor.toll.com.au') conn.putheader('Accept', 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*') conn.putheader('Accept-Language', 'en') conn.putheader('Accept-Charset', 'iso-8859-1,*,utf-8') conn.endheaders() response = conn.getresponse() data = response.read() conn.close() print "Status %s Reason \"%s\"" % (response.status, response.reason) print "Data \"%s\"" % data #--------------------------------------------------------------------- # HTTPD Status # # 200 - OK # 302 - Moved temporarily (a redirect) # 404 - Server failure (page not found) # #---------------------------------------------------------------------