Changeset 80


Ignore:
Timestamp:
06/12/15 17:25:01 (10 years ago)
Author:
tim
Message:

fixed a few problems with non-requests scripts
made HTTPS and port guessing a little smarter based on the Referer header
more comments in output

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/bletchley-http2py

    r79 r80  
    3737import sys
    3838import argparse
     39import pprint
     40import urllib.parse
    3941
    4042bopen = lambda f: open(f, 'rb')
     
    5153parser.add_argument(
    5254    '--requests', action='store_true', help='Generate a script that uses the'
    53     ' python-requests module rather than httplib/http.client (experimental).')
     55    ' python-requests module rather than http.client (this will likely become'
     56    ' the default in the future).')
    5457
    5558args = parser.parse_args()
     
    7174
    7275host = 'TODO'
    73 port = 80
    74 use_ssl = False
    75 protocol = 'http'
     76port = None
     77protocol = None
    7678
    7779headers = []
     
    103105            port = int(port, 10)
    104106            if port == 443:
    105                 use_ssl = True
    106107                protocol = 'https'
    107108        else:
    108109            host = value
    109110
    110 
     111    # Attempt to guess the port and protocol from the referer header, since
     112    # often it is the same site.  Defer to the host header though, if the
     113    # info is there.
     114    elif name.lower() == 'referer':
     115        rurl = urllib.parse.urlparse(value)
     116        if rurl.netloc == host:
     117            if rurl.scheme == 'https' and protocol == None:
     118                protocol = 'https'
     119            if rurl.port != None and port == None:
     120                port = rurl.port
     121
     122if protocol == None:
     123    protocol == 'http'       
     124if port == None:
     125    if protocol == 'https':
     126        port = 443
     127    else:
     128        port = 80
     129
     130
     131# XXX: use pprint
    111132formatted_body = '\n            '.join([repr(body[i:i+40]) for i in range(0,len(body),40)])
    112133if formatted_body == '':
     
    123144from bletchley.CBC import *
    124145
    125 # TODO: ensure the host, port, and SSL settings are correct.
     146# TODO: ensure the host, port, and protocol settings are correct.
    126147host = %s
    127148port = %s
     
    169190    body = (%s)
    170191
    171     return session.request(method, url, headers=headers, data=body, allow_redirects=False)
    172     ''' % (repr(method), repr(path), repr(headers), formatted_body))
     192    # Set verify=True if you want to validate the server cert
     193    return session.request(method, url, headers=headers, data=body, allow_redirects=False, verify=False)
     194    ''' % (repr(method), repr(path), pprint.pformat(headers, indent=14), formatted_body))
    173195
    174196    print('''   
     
    185207    # These are useful for debugging, but once your response processing is working,
    186208    # remove them so it isn't so verbose.
     209    print(response.status_code)
    187210    print(response.headers)
    188211    print(repr(response.content))
     
    192215
    193216
    194 
    195217else:
    196218    print('''
    197 import http.client as httpc
     219import http.client
    198220
    199221def sendRequest(connection, data=None):
     
    224246
    225247def newConnection():
    226     if use_ssl:
    227         return httpc.HTTPSConnection(host, port)
     248    global protocol
     249    if protocol == 'https':
     250        return http.client.HTTPSConnection(host, port)
    228251    else:
    229         return httpc.HTTPConnection(host, port)
     252        return http.client.HTTPConnection(host, port)
    230253
    231254
     
    241264    # These are useful for debugging, but once your response processing is working,
    242265    # remove them so it isn't so verbose.
     266    print(response.status)
    243267    print(response.getheaders())
    244268    print(repr(response.read()))
Note: See TracChangeset for help on using the changeset viewer.