Difference between revisions of "Proxy basic.py"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
Line 156: Line 156:
"""
"""
</pre>
</pre>
[[Category:Python]]
[[Category:Python]]
[[Category:. httplib]]
[[Category:Python httplib]]
[[Category:Examples]]
[[Category:Examples]]

Latest revision as of 15:07, 1 August 2015

Script

$ cat proxy_basic.py 
#!/usr/bin/env python
#
#
#
#-------------------------------------------------------------------------------

import sys
import base64
import httplib
import urllib2

#-------------------------------------------------------------------------------

def do():
   proxy_support = urllib2.ProxyHandler({"http":"http://proxy:8080"})

   http_support = urllib2.HTTPHandler()

   opener = urllib2.build_opener(proxy_support, http_support)

   urllib2.install_opener(opener)

   connf = urllib2.urlopen('http://www.python.org/')

   auth = "Basic " + base64.encodestring("xxx:xxx")

   conn.putheader('Proxy-Authorization', auth)
   conn.endheaders()

   #errcode, errmsg, headers = conn.getresponse()
   rc = conn.getresponse()

   # print errcode
   # print errmsg
   # print headers

   # f = conn.getfile()

   # for line in f.readlines():
   #    print line

   # rc = conn.getresponse()

   print rc.__dict__
   print rc.status, rc.reason
   print rc.msg

   data = rc.read()

   # print data

   conn.close()


#-------------------------------------------------------------------------------

def main(args):
   do()

#-------------------------------------------------------------------------------

if __name__ == "__main__":
   main(sys.argv[1:])

#-------------------------------------------------------------------------------

NTLM Needed!

cat proxy_get.py
#!/usr/bin/env python
#
#
#
#-------------------------------------------------------------------------------

import sys
import base64
import httplib

#-------------------------------------------------------------------------------

passwd = 'xxx'

#-------------------------------------------------------------------------------

def do():
   conn = httplib.HTTPConnection('10.10.10.10', 8080)

   conn.putrequest("GET", "http://www.google.com.au/")

   conn.putheader('Accept', 'text/html')
   conn.putheader('Accept', 'text/plain')

   auth = "Basic " + base64.encodestring("name:%s" % passwd)

   conn.putheader('Proxy-Authorization', auth)
   conn.endheaders()

   #errcode, errmsg, headers = conn.getresponse()
   rc = conn.getresponse()

   # print errcode
   # print errmsg
   # print headers

   # f = conn.getfile()

   # for line in f.readlines():
      # print line

   # rc = conn.getresponse()

   print rc.__dict__
   print rc.status, rc.reason
   print rc.msg

   data = rc.read()

   # print data

   conn.close()

#-------------------------------------------------------------------------------

def main(args):
   do()

#-------------------------------------------------------------------------------

if __name__ == "__main__":
   main(sys.argv[1:])

#-------------------------------------------------------------------------------

"""
$ ./proxy_get.py 
{'fp': <socket._fileobject object at 0x7fedc09c>, 'status': 407, 'will_close': True, 'chunk_left': 'UNKNOWN', 'length': 2377, 'strict': 0, 'reason': 'Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )', 'version': 11, 'debuglevel': 0, 'msg': <httplib.HTTPMessage instance at 0x7ff4178c>, 'chunked': 0, '_method': 'GET'}
407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied.  )
Via:1.1 PROXY
Proxy-Authenticate: NTLM
Proxy-Authenticate: Basic realm="PROXY.dmz.performiq.com.au"
Proxy-Authenticate: Kerberos
Proxy-Authenticate: Negotiate
Connection: close
Proxy-Connection: close
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 2377 
"""