def request(url=None, method=None, timeout=None, proxy=None, client=None): parsed_url = urlparse(url) scheme = parsed_url.scheme host = parsed_url.netloc if scheme == 'http': port = 80 else: port = 443 tunnelHttpAgent = TunnelHttpAgent( host='104.248.43.30', port=1337, server_name=host, server_port=port, auth=auth, timeout=timeout, proxy=proxy, client=client, ) connection = HTTPConnection(host=host, port=port, timeout=timeout) connection.sock = tunnelHttpAgent.sock connection.request(method=method, url=parsed_url.path, body=parsed_url.query, headers=headers) return connection
def _do_read(self, query, raw=False): # send query to server, return JSON rethinker = doublethink.Rethinker(db="trough_configuration", servers=self.rethinkdb) healthy_databases = list( rethinker.table('services').get_all(self.database, index='segment').run()) healthy_databases = [ db for db in healthy_databases if db['role'] == 'trough-read' and (rethinker.now().run() - db['last_heartbeat']).seconds < db['ttl'] ] try: assert len(healthy_databases) > 0 except: raise Exception('No healthy node found for segment %s' % self.database) url = urlparse(healthy_databases[0].get('url')) if self.proxy: conn = HTTPConnection(self.proxy, self.proxy_port) conn.set_tunnel(url.netloc, url.port) conn.sock = socks.socksocket() conn.sock.set_proxy(self.proxy_type, self.proxy, self.proxy_port) conn.sock.connect((url.netloc.split(":")[0], url.port)) else: conn = HTTPConnection(url.netloc) request_path = "%s?%s" % (url.path, url.query) conn.request("POST", request_path, query) response = conn.getresponse() results = json.loads(response.read()) self._last_results = results
def http(self, environ, start_response): try: method, url, body, headers = copy_request(environ) host, port = get_destination(environ) except Exception as e: log.error("[Exception][http]: %s" % str(e)) start_response("400 Bad Request", [("Content-Type", "text/plain; charset=utf-8")]) yield "Bad Request" return socksconn = self.connect_socks(host, port) if not socksconn: start_response("500 Internal Server Error", [("Content-Type", "text/plain; charset=utf-8")]) yield "Internal Server Error" return try: conn = HTTPConnection(host, port=port) conn.sock = socksconn set_forwarded_for(environ, headers) u = urllib.parse.urlsplit(url) path = urllib.parse.urlunsplit(("", "", u.path, u.query, "")) conn.request(method, path, body, headers) resp = conn.getresponse() start_response("%d %s" % (resp.status, resp.reason), resp.getheaders()) while True: data = resp.read(CHUNKSIZE) if not data: break yield data conn.close() except Exception as e: log.error("[Exception][http]: %s" % str(e)) start_response("500 Internal Server Error", [("Content-Type", "text/plain; charset=utf-8")]) yield "Internal Server Error" return
def http(self, environ, start_response): try: host, port = get_destination(environ) log.info("HTTP request to (%s:%d)" % (host, port)) if host == '127.0.0.1': status = '200 OK' response_headers = [('Content-Type', 'text/plain')] start_response(status, response_headers) yield b'It\'s Work!' return method, url, body, headers = copy_request(environ) if 'wxclient/app/attendance/addForEc' in url: if method == 'POST': headers[ 'User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0_3 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A432 MicroMessenger/6.5.21 NetType/WIFI Language/zh_CN' body = u'{"signTime":"2017-11-08 14:15:28","success":false,"msg":"温馨提示:您的地理位置距离签到地点超过最小签到范围限制的500米","btnType":"2"}' status = '200 OK' response_headers = [ ('Content-Type', 'application/json;charset=UTF-8'), ('Content-Length', str(len(str.encode(body)))) ] start_response(status, response_headers) yield str.encode(body) return except Exception as e: log.error("[Exception][http 400]: %s" % str(e)) start_response("400 Bad Request", [("Content-Type", "text/plain; charset=utf-8")]) yield "Bad Request" return try: # dismiss x-forwarders # set_forwarded_for(environ, headers) http_conn = socket.create_connection((host, port), timeout=self.timeout) conn = HTTPConnection(host, port=port) conn.sock = http_conn if '/app/attendance/js/sign_ec.js' in url: # url = '/app/attendance/js/sign_ec.js?'+str(random.randint(0, 9999)) # headers['If-None-Match'] = 'W/"10040-1502791023965"' print('**********', url, '**********') with open(sys.path[0] + "/sign_ec.js", 'rb') as sign_ec_js: data = sign_ec_js.read() status = '200 OK' response_headers = [ ('X-Frame-Options', 'SAMEORIGIN'), ('Accept-Ranges', 'bytes'), ('Cache-Control', 'public, max-age=31536000'), ('Content-Type', 'application/javascript'), ('Date', 'Thu, 09 Nov 2017 05:54:54 GMT'), ('ETag', 'W/"10040-1502791023964"'), ('Last-Modified', 'Tue, 15 Aug 2017 09:57:03 GMT'), ('Server', 'Apache-Coyote/1.1'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) yield data conn.close() return u = urllib.parse.urlsplit(url) path = urllib.parse.urlunsplit(("", "", u.path, u.query, "")) # Host header put by conn.request conn.request(method, path, body, headers) resp = conn.getresponse() headers = resp.getheaders() for header in headers: if 'Content-Length' in header: headers.remove(header) chunked_encoding = ('Transfer-Encoding', 'chunked') headers.append(chunked_encoding) if '/app/attendance/js/sign_ec.js' in url: # url = '/app/attendance/js/sign_ec.js?'+str(random.randint(0, 9999)) # headers['If-None-Match'] = 'W/"10040-1502791023965"' log.info('Respone for %s' % url) for header in headers: if 'Cache-Control' in header: headers.remove(header) CacheControl = ('Cache-Control', 'public,max-age=31536000') headers.append(CacheControl) print(url) start_response("%d %s" % (resp.status, resp.reason), headers) while True: data = resp.read(CHUNKSIZE) if not data: break if '/app/attendance/js/sign_ec.js' in url: data = bytes.decode(data) with open(sys.path[0] + "/config.json", 'r') as load_f: load_dict = json.load(load_f) # lng = float('%s%d' % (load_dict['lng'], random.randint(0, 999))) # lat = float('%s%d' % (load_dict['lat'], random.randint(0, 999))) # print(lng, ',', lat) data = re.sub( r'setLngLat\(lng, lat\) {', 'setLngLat(lng, lat) {\n\tlng=%s+Math.random()*1000/1000000;\n\tlat=%s+Math.random()*1000/1000000;\n' % (load_dict['lng'], load_dict['lat']), data) data = re.sub(r'定位点', '牛X的定位点', data) ''' data = re.sub(r'longitude = position.coords.longitude;', 'longitude = 116.404731;', data) data = re.sub(r'latitude = position.coords.latitude;', 'latitude = 39.905679";', data) ''' log.info(url + 'REPLACE++++++++++') data = str.encode(data) if 'wxclient/app/attendance/addForEc' in url: if method == 'POST': data = bytes.decode(data) log.info(data) data = str.encode(data) yield data conn.close() except Exception as e: log.error("[Exception][http 500]: %s" % str(e)) start_response("500 Internal Server Error", [("Content-Type", "text/plain; charset=utf-8")]) yield "Internal Server Error" return
def socket_http_factory(cls, host): http = HTTPConnection(host, 80) http.sock = cls.socket_conn(cls.server) return http