Changeset 80
- Timestamp:
- 06/12/15 17:25:01 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bin/bletchley-http2py
r79 r80 37 37 import sys 38 38 import argparse 39 import pprint 40 import urllib.parse 39 41 40 42 bopen = lambda f: open(f, 'rb') … … 51 53 parser.add_argument( 52 54 '--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).') 54 57 55 58 args = parser.parse_args() … … 71 74 72 75 host = 'TODO' 73 port = 80 74 use_ssl = False 75 protocol = 'http' 76 port = None 77 protocol = None 76 78 77 79 headers = [] … … 103 105 port = int(port, 10) 104 106 if port == 443: 105 use_ssl = True106 107 protocol = 'https' 107 108 else: 108 109 host = value 109 110 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 122 if protocol == None: 123 protocol == 'http' 124 if port == None: 125 if protocol == 'https': 126 port = 443 127 else: 128 port = 80 129 130 131 # XXX: use pprint 111 132 formatted_body = '\n '.join([repr(body[i:i+40]) for i in range(0,len(body),40)]) 112 133 if formatted_body == '': … … 123 144 from bletchley.CBC import * 124 145 125 # TODO: ensure the host, port, and SSLsettings are correct.146 # TODO: ensure the host, port, and protocol settings are correct. 126 147 host = %s 127 148 port = %s … … 169 190 body = (%s) 170 191 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)) 173 195 174 196 print(''' … … 185 207 # These are useful for debugging, but once your response processing is working, 186 208 # remove them so it isn't so verbose. 209 print(response.status_code) 187 210 print(response.headers) 188 211 print(repr(response.content)) … … 192 215 193 216 194 195 217 else: 196 218 print(''' 197 import http.client as httpc219 import http.client 198 220 199 221 def sendRequest(connection, data=None): … … 224 246 225 247 def 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) 228 251 else: 229 return http c.HTTPConnection(host, port)252 return http.client.HTTPConnection(host, port) 230 253 231 254 … … 241 264 # These are useful for debugging, but once your response processing is working, 242 265 # remove them so it isn't so verbose. 266 print(response.status) 243 267 print(response.getheaders()) 244 268 print(repr(response.read()))
Note: See TracChangeset
for help on using the changeset viewer.