hackRequests是主要http请求函数。 可以通过http或者httpraw来访问网络
| 102 | |
| 103 | |
| 104 | class hackRequests(object): |
| 105 | ''' |
| 106 | hackRequests是主要http请求函数。 |
| 107 | |
| 108 | 可以通过http或者httpraw来访问网络 |
| 109 | ''' |
| 110 | |
| 111 | def __init__(self, conpool=None): |
| 112 | self.lock = threading.Lock() |
| 113 | |
| 114 | if conpool is None: |
| 115 | self.httpcon = httpcon(timeout=17) |
| 116 | else: |
| 117 | self.httpcon = conpool |
| 118 | |
| 119 | def _get_urlinfo(self, url, realhost: str): |
| 120 | p = parse.urlparse(url) |
| 121 | scheme = p.scheme.lower() |
| 122 | if scheme != "http" and scheme != "https": |
| 123 | raise Exception("http/https only") |
| 124 | hostname = p.netloc |
| 125 | port = 80 if scheme == "http" else 443 |
| 126 | if ":" in hostname: |
| 127 | hostname, port = hostname.split(":") |
| 128 | path = "" |
| 129 | if p.path: |
| 130 | path = p.path |
| 131 | if p.query: |
| 132 | path = path + "?" + p.query |
| 133 | if realhost: |
| 134 | if ":" not in realhost: |
| 135 | realhost = realhost + ":80" |
| 136 | hostname, port = realhost.split(":") |
| 137 | return scheme, hostname, int(port), path |
| 138 | |
| 139 | def _send_output(self, oldfun, con, log): |
| 140 | def _send_output_hook(*args, **kwargs): |
| 141 | log['request'] = b"\r\n".join(con._buffer).decode('utf-8') |
| 142 | oldfun(*args, **kwargs) |
| 143 | con._send_output = oldfun |
| 144 | |
| 145 | return _send_output_hook |
| 146 | |
| 147 | def httpraw(self, raw: str, **kwargs): |
| 148 | raw = raw.strip() |
| 149 | proxy = kwargs.get("proxy", None) |
| 150 | real_host = kwargs.get("real_host", None) |
| 151 | ssl = kwargs.get("ssl", False) |
| 152 | |
| 153 | scheme = 'http' |
| 154 | port = 80 |
| 155 | if ssl: |
| 156 | scheme = 'https' |
| 157 | port = 443 |
| 158 | |
| 159 | try: |
| 160 | index = raw.index('\n') |
| 161 | except ValueError: |