home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2490 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.1 KB  |  109 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import urllib
  6. import httplib
  7. import urlparse
  8. from base64 import encodestring, decodestring
  9. from sspi import ClientAuth
  10. import optparse
  11. options = None
  12.  
  13. def open_url(host, url):
  14.     h = httplib.HTTPConnection(host)
  15.     h.putrequest('GET', url)
  16.     h.endheaders()
  17.     resp = h.getresponse()
  18.     print 'Initial response is', resp.status, resp.reason
  19.     body = resp.read()
  20.     if resp.status == 302:
  21.         url = '/' + resp.msg['location']
  22.         resp.close()
  23.         h.putrequest('GET', url)
  24.         h.endheaders()
  25.         resp = h.getresponse()
  26.         print 'After redirect response is', resp.status, resp.reason
  27.     
  28.     if options.show_headers:
  29.         print 'Initial response headers:'
  30.         for name, val in resp.msg.items():
  31.             print ' %s: %s' % (name, val)
  32.         
  33.     
  34.     if options.show_body:
  35.         print body
  36.     
  37.     if resp.status == 401:
  38.         auth_info = None
  39.         if options.user and options.domain or options.password:
  40.             auth_info = (options.user, options.domain, options.password)
  41.         
  42.         ca = ClientAuth('NTLM', auth_info = auth_info)
  43.         auth_scheme = ca.pkg_info['Name']
  44.         data = None
  45.         for s in resp.msg.get('WWW-Authenticate', '').split(','):
  46.             schemes = _[1][s.strip()]
  47.             for scheme in schemes:
  48.                 if scheme.startswith(auth_scheme):
  49.                     data = decodestring(scheme[len(auth_scheme) + 1:])
  50.                     break
  51.                     continue
  52.                 _[1]
  53.             else:
  54.                 print "Could not find scheme '%s' in schemes %r" % (auth_scheme, schemes)
  55.                 break
  56.             resp.read()
  57.     []
  58.     print 'Final response status is', resp.status, resp.reason
  59.     if resp.status == 200:
  60.         if resp.will_close:
  61.             print 'EEEK - response will close, but NTLM is per connection - it must stay open'
  62.         
  63.         body = resp.read()
  64.         if options.show_body:
  65.             print 'Final response body:'
  66.             print body
  67.         
  68.         h.putrequest('GET', url)
  69.         h.endheaders()
  70.         resp = h.getresponse()
  71.         print 'Second fetch response is', resp.status, resp.reason
  72.         if options.show_headers:
  73.             print 'Second response headers:'
  74.             for name, val in resp.msg.items():
  75.                 print ' %s: %s' % (name, val)
  76.             
  77.         
  78.         resp.read(int(resp.msg.get('content-length', 0)))
  79.     elif resp.status == 500:
  80.         print 'Error text'
  81.         print resp.read()
  82.     elif options.show_body:
  83.         cl = resp.msg.get('content-length')
  84.         print resp.read(int(cl))
  85.     
  86.  
  87. if __name__ == '__main__':
  88.     parser = optparse.OptionParser(description = __doc__)
  89.     parser.add_option('', '--show-body', action = 'store_true', help = 'print the body of each response as it is received')
  90.     parser.add_option('', '--show-headers', action = 'store_true', help = 'print the headers of each response as it is received')
  91.     parser.add_option('', '--user', action = 'store', help = 'The username to login with')
  92.     parser.add_option('', '--password', action = 'store', help = 'The password to login with')
  93.     parser.add_option('', '--domain', action = 'store', help = 'The domain to login to')
  94.     (options, args) = parser.parse_args()
  95.     if not args:
  96.         print 'Run with --help for usage details'
  97.         args = [
  98.             'http://localhost/localstart.asp']
  99.     
  100.     for url in args:
  101.         (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
  102.         if scheme != 'http' and params and query or fragment:
  103.             parser.error('Scheme must be http, URL must be simple')
  104.         
  105.         print "Opening '%s' from '%s'" % (path, netloc)
  106.         r = open_url(netloc, path)
  107.     
  108.  
  109.