def make_request(method, uri, headers=None, body=None): """ This function should make a request, connection reuse, etc. is left to the implementation :param str|unicode method: HTTP Verb :param str|unicode uri: Server/path to make the request to :param dict headers: Headers to send :param str|unicode body: Body to send :return: Request result :rtype: Tuple of return_code (int), headers (list of (k,v) tuples), body (str|bytes) """ parsed_url = urlparse(uri) if parsed_url.scheme == 'https': connector = HTTPSConnection(host=parsed_url.netloc) elif parsed_url.scheme == 'http': connector = HTTPConnection(host=parsed_url.netloc) else: raise ValueError('Schema is not HTTP nor HTTPS') _, full_path = uri.split(parsed_url.netloc, 1) connector.request(method=method, url=full_path, body=body, headers=headers or {}) response = connector.getresponse() status_code = response.status body = response.read() headers = response.getheaders() return status_code, headers, body
def predict(self, examples): """Run prediction over HTTP/REST. :param examples: The input examples :return: The outcomes """ valid_example = all(k in examples for k in self.input_keys) if not valid_example: raise ValueError("should have keys: " + ",".join(self.input_keys)) v_str = '/versions/{}'.format( self.version) if self.version is not None else '' path = '/v1/models/{}{}:predict'.format(self.name, v_str) request = self.create_request(examples) headers = {'Content-type': 'application/json'} _, hostname, port = self.remote.split(':') conn = HTTPConnection(hostname.replace('//', ''), port) conn.request('POST', path, json.dumps(request), headers) response = conn.getresponse().read() outcomes_list = json.loads(response) if "error" in outcomes_list: raise ValueError("remote server returns error: {0}".format( outcomes_list["error"])) outcomes_list = outcomes_list["outputs"] outcomes_list = self.deserialize_response(examples, outcomes_list) return outcomes_list
def report_test_status(self, name, status, tags=[], remote_url=''): """Report test status and tags to SauceLabs """ job_id = BuiltIn().get_library_instance( 'ExtendedSelenium2Library')._current_browser().session_id if USERNAME_ACCESS_KEY.match(remote_url): username, access_key =\ USERNAME_ACCESS_KEY.findall(remote_url)[0][1:] else: username = os.environ.get('SAUCE_USERNAME') access_key = os.environ.get('SAUCE_ACCESS_KEY') if not job_id: return u"No Sauce job id found. Skipping..." elif not username or not access_key: return u"No Sauce environment variables found. Skipping..." token = base64.encodestring('%s:%s' % (username, access_key))[:-1] body = json.dumps({'name': name, 'passed': status == 'PASS', 'tags': tags}) connection = HTTPConnection('saucelabs.com') connection.request('PUT', '/rest/v1/%s/jobs/%s' % ( username, job_id), body, headers={'Authorization': 'Basic %s' % token} ) return connection.getresponse().status
def send_email(request): try: recipients = request.GET['to'].split(',') url = request.GET['url'] proto, server, path, query, frag = urlsplit(url) if query: path += '?' + query conn = HTTPConnection(server) conn.request('GET',path) try: # Python 2.7+, use buffering of HTTP responses resp = conn.getresponse(buffering=True) except TypeError: # Python 2.6 and older resp = conn.getresponse() assert resp.status == 200, "Failed HTTP response %s %s" % (resp.status, resp.reason) rawData = resp.read() conn.close() message = MIMEMultipart() message['Subject'] = "Graphite Image" message['To'] = ', '.join(recipients) message['From'] = 'composer@%s' % gethostname() text = MIMEText( "Image generated by the following graphite URL at %s\r\n\r\n%s" % (ctime(),url) ) image = MIMEImage( rawData ) image.add_header('Content-Disposition', 'attachment', filename="composer_" + strftime("%b%d_%I%M%p.png")) message.attach(text) message.attach(image) s = SMTP(settings.SMTP_SERVER) s.sendmail('composer@%s' % gethostname(),recipients,message.as_string()) s.quit() return HttpResponse( "OK" ) except Exception: return HttpResponse(format_exc())
def retry_http_krb_sspi_auth(self, host, req): url = req.full_url scheme, _, host, path = url.split('/', 3) h = HTTPConnection(host) if scheme == 'http:' else HTTPSConnection( host) headers = dict(req.unredirected_hdrs) headers.update( dict((k, v) for k, v in req.headers.items() if k not in headers)) try: __, krb_context = kerberos.authGSSClientInit("HTTP@" + host) kerberos.authGSSClientStep(krb_context, "") negotiate_details = kerberos.authGSSClientResponse(krb_context) headers["Connection"] = "Keep-Alive" headers["Authorization"] = "Negotiate " + negotiate_details h.request(req.get_method(), req.selector, req.data, headers) response = h.getresponse() return addinfourl(response, response.msg, req.get_full_url(), response.status) except: # e = sys.exc_info()[0] # _log.warning(str(e)) # _log.warning('Failed Kerberos authentication') return None
def _compute_engine_id(): """Gets the Compute Engine project ID if it can be inferred. Uses 169.254.169.254 for the metadata server to avoid request latency from DNS lookup. See https://cloud.google.com/compute/docs/metadata#metadataserver for information about this IP address. (This IP is also used for Amazon EC2 instances, so the metadata flavor is crucial.) See https://github.com/google/oauth2client/issues/93 for context about DNS latency. :rtype: string or ``NoneType`` :returns: Compute Engine project ID if the metadata service is available, else ``None``. """ host = '169.254.169.254' uri_path = '/computeMetadata/v1/project/project-id' headers = {'Metadata-Flavor': 'Google'} connection = HTTPConnection(host, timeout=0.1) try: connection.request('GET', uri_path, headers=headers) response = connection.getresponse() if response.status == 200: return response.read() except socket.error: # socket.timeout or socket.error(64, 'Host is down') pass finally: connection.close()
def _download_http_file(self, url, target_path): '''下载http文件 :param url: HTTP路径 :type url: string :param target_path: 下载到目标路径 :type target_path: string ''' url0 = url if url[:7] == 'http://': url = url[7:] pos = url.find('/') host = url[:pos] page = url[pos:] conn = HTTPConnection(host, port=80, timeout=60) # 60秒超时 conn.request('GET', page) res = conn.getresponse() if res.status != 200: raise RuntimeError('访问:%s 错误[HTTP错误码:%s]' % (url0, res.status)) target_size = int(res.getheader('Content-Length')) data = res.read() conn.close() if len(data) != target_size: return self._download_http_file(url0, target_path) else: f = open(target_path, 'wb') f.write(data) f.close()
def __init__(self, service_uri, credentials=None, key_file=None, cert_file=None, timeout=10, buffer_size=65535, error_factory=lambda x: x, overload=False, version=1): self.version = version self._headers = list() self._headers.append(( 'User-Agent', 'python-hessian/' + __version__, )) self._headers.append(( 'Content-Type', 'application/x-hessian', )) if sys.version_info < (2, 6): warn('HessianProxy timeout not enforceable before Python 2.6', RuntimeWarning, stacklevel=2) kwargs = {} else: kwargs = {'timeout': timeout} if six.PY2: kwargs['strict'] = True self._uri = urlparse(service_uri) if self._uri.scheme == 'http': self._client = HTTPConnection(self._uri.hostname, self._uri.port or 80, **kwargs) elif self._uri.scheme == 'https': self._client = HTTPSConnection(self._uri.hostname, self._uri.port or 443, key_file=key_file, cert_file=cert_file, **kwargs) else: raise NotImplementedError( "HessianProxy only supports http:// and https:// URIs") # autofill credentials if they were passed via url instead of kwargs if (self._uri.username and self._uri.password) and not credentials: credentials = (self._uri.username, self._uri.password) if credentials: auth = 'Basic ' + base64.b64encode(':'.join(credentials)) self._headers.append(('Authorization', auth)) self._buffer_size = buffer_size self._error_factory = error_factory self._overload = overload self._parser = Parser()
def _get_conn(self): if self._conn is not None: return self._conn host, port = self._daemon.split(':', 2) port = int(port) self._conn = HTTPConnection(host, port, timeout=self._timeout) return self._conn
def sneaky_setting_host(): # We don't handle that the host is overwritten directly. # A contrived example; you're not supposed to do this, but you certainly can. fake = 'fakehost.com' real = 'realhost.com' conn = HTTPConnection(fake) conn.host = real conn.request('GET', '/path')
def _check_storage(ipport, path): conn = HTTPConnection(*ipport) conn.request('GET', path) resp = conn.getresponse() # 404 because it's a nonsense path (and mount_check is false) # 507 in case the test target is a VM using mount_check if resp.status not in (404, 507): raise Exception('Unexpected status %s' % resp.status) return resp
def set_test_status(self, passed=True): connection = HTTPConnection("saucelabs.com") connection.request( 'PUT', '/rest/v1/{0}/jobs/{1}'.format(self.username, self.driver.session_id), json.dumps({"passed": passed}), headers={"Authorization": "Basic {0}".format(self.sauce_auth)}) result = connection.getresponse() return result.status == 200
def _get_conn(self): if not self.connected: return None if self._conn is not None: return self._conn host, port = self.master.split(':', 2) port = int(port) self._conn = HTTPConnection(host, port, timeout=self._timeout) return self._conn
def export_to_myexp(self, trans, id, myexp_username, myexp_password): """ Exports a workflow to myExperiment website. """ trans.workflow_building_mode = workflow_building_modes.ENABLED stored = self.get_stored_workflow(trans, id, check_ownership=False, check_accessible=True) # Convert workflow to dict. workflow_dict = self._workflow_to_dict(trans, stored) # # Create and submit workflow myExperiment request. # # Create workflow content JSON. workflow_content = json.dumps(workflow_dict, indent=4, sort_keys=True) # Create myExperiment request. request_raw = trans.fill_template( "workflow/myexp_export.mako", workflow_name=workflow_dict['name'], workflow_description=workflow_dict['annotation'], workflow_content=workflow_content, workflow_svg=self._workflow_to_svg_canvas(trans, stored).tostring() ) # strip() b/c myExperiment XML parser doesn't allow white space before XML; utf-8 handles unicode characters. request = unicodify(request_raw.strip(), 'utf-8') # Do request and get result. auth_header = base64.b64encode('%s:%s' % (myexp_username, myexp_password)) headers = {"Content-type": "text/xml", "Accept": "text/xml", "Authorization": "Basic %s" % auth_header} myexp_url = trans.app.config.myexperiment_target_url conn = HTTPConnection(myexp_url) # NOTE: blocks web thread. conn.request("POST", "/workflow.xml", request, headers) response = conn.getresponse() response_data = response.read() conn.close() # Do simple parse of response to see if export successful and provide user feedback. parser = SingleTagContentsParser('id') parser.feed(response_data) myexp_workflow_id = parser.tag_content workflow_list_str = " <br>Return to <a href='%s'>workflow list." % url_for(controller='workflows', action='list') if myexp_workflow_id: return trans.show_message( """Workflow '%s' successfully exported to myExperiment. <br/> <a href="http://%s/workflows/%s">Click here to view the workflow on myExperiment</a> %s """ % (stored.name, myexp_url, myexp_workflow_id, workflow_list_str), use_panels=True) else: return trans.show_error_message( "Workflow '%s' could not be exported to myExperiment. Error: %s %s" % (stored.name, response_data, workflow_list_str), use_panels=True)
def read(self, size): protocol, host, port, path = self.urlsplit(self.url) conn = HTTPConnection(host, port) headers = { 'Range': 'bytes=%d-%d' % (self.offset, self.offset + size - 1) } conn.request('GET', path, None, headers) response = conn.getresponse() data = response.read() self.offset += len(data) return data
def connection(self): """Gets the current HTTP connection, or lazily creates one.""" if not self._conn: conn_kwargs = {} if not PY3: conn_kwargs["strict"] = True # We are not setting an HTTP timeout other than the default when the # connection its created. The send method has a timeout value if needed. self._conn = HTTPConnection(self.host, self.port, **conn_kwargs) return self._conn
def connection(self): """Gets the current HTTP connection, or lazily creates one.""" if not self._conn: conn_kwargs = {} if self._timeout is not None: conn_kwargs["timeout"] = self._timeout if not PY3: conn_kwargs["strict"] = True self._conn = HTTPConnection(self.host, self.port, **conn_kwargs) return self._conn
def test_reconnect(): a = HTTPConnection('127.0.0.1') b = io_datafind.reconnect(a) assert b is not a assert b.host == a.host assert b.port == a.port with mock.patch('os.walk', return_value=[]): a = io_datafind.FflConnection() b = io_datafind.reconnect(a) assert b is not a assert b.ffldir == a.ffldir
def connection(self): """Gets the current HTTP connection, or lazily creates one.""" if not self._conn: conn_kwargs = {} if not PY3: conn_kwargs["strict"] = True # We are not setting an HTTP timeout other than the default # because the timeouts are handled externally by the runner # and can be different for each type of test. self._conn = HTTPConnection(self.host, self.port, **conn_kwargs) return self._conn
def wait_for_server_to_hangup(ipport): try_until = time() + 30 while True: try: conn = HTTPConnection(*ipport) conn.request('GET', '/') conn.getresponse() except Exception: break if time() > try_until: raise Exception( 'Still answering on %s:%s after 30 seconds' % ipport) sleep(0.1)
def is_http_running_on(port): """ Check if an http server runs on a given port. Args: The port to check. Returns: True if it is used by an http server. False otherwise. """ try: conn = HTTPConnection('127.0.0.1:' + str(port)) conn.connect() conn.close() return True except Exception: return False
def execute_request(self): """"Execute the request, and get a response. Returns: an HttpResponse object from httplib """ if self.is_secure_connection: conn = HTTPSConnection(self.get_hostname(), timeout=self.timeout) else: conn = HTTPConnection(self.get_hostname(), timeout=self.timeout) conn.request(self.method, self.url, self.body, self.headers) response = conn.getresponse() return response
def open_http_connection(self, code, query=None, method='GET'): """ Send a request to non-sandboxed Splash, return an HTTPConnection. create_file Lua function is pre-loaded. XXX: why can't we use requests or urllib, why don't they close a connection after a timeout error? """ q = {"lua_source": self.CREATE_FILE + "\n" + code} q.update(query or {}) conn = HTTPConnection('localhost', self.splash_unrestricted.portnum) conn.request(method, "/execute/?" + six.moves.urllib.parse.urlencode(q)) return conn
def getSuggestions(self, queryString): self.prepareQuery() if queryString != "": query = self.prepQuerry + quote(queryString) self.conn = HTTPConnection("google.com") try: self.conn = HTTPConnection("google.com") self.conn.request("GET", query, "", {"Accept-Encoding": "UTF-8"}) except (CannotSendRequest, gaierror, error): self.conn.close() print("[MyTube - GoogleSuggestions] Can not send request for suggestions") return None else: try: response = self.conn.getresponse() except BadStatusLine: self.conn.close() print("[MyTube - GoogleSuggestions] Can not get a response from google") return None else: if response.status == 200: data = response.read() header = response.getheader("Content-Type", "text/xml; charset=ISO-8859-1") charset = "ISO-8859-1" try: charset = header.split(";")[1].split("=")[1] print("[MyTube - GoogleSuggestions] Got charset %s" % charset) except: print("[MyTube - GoogleSuggestions] No charset in Header, falling back to %s" % charset) data = data.decode(charset).encode("utf-8") self.conn.close() return data else: self.conn.close() return None else: return None
def is_alive(self): """Test that the connection is still alive. Because the remote communication happens over HTTP we need to make an explicit request to the remote. It is allowed for WebDriver spec tests to not have a WebDriver session, since this may be what is tested. An HTTP request to an invalid path that results in a 404 is proof enough to us that the server is alive and kicking. """ conn = HTTPConnection(self.server.host, self.server.port) conn.request("HEAD", self.server.base_path + "invalid") res = conn.getresponse() return res.status == 404
def __init__(self, url, max_rate=3.0, rate_bucket_size=None, socket_timeout=10, user_agent=DEFAULT_USER_AGENT): self.url = url netloc, self.path = _parse_url(url) self.conn = HTTPConnection(netloc, timeout=socket_timeout) self.request_headers = self.request_headers.copy() self.request_headers['User-Agent'] = user_agent self.stream = None self.rate_limiter = BucketRateLimiter(max_rate, rate_bucket_size) self.open_rate_limiter = BackoffRateLimiter(socket_timeout)
def _findMatchUrl(self, tag): h3 = tag.find('h3') if not h3: return '' a = h3.find('a') url = a.attrs.get('href', '') # decode url host = parse.urlsplit(url).netloc path = url[len(parse.urljoin(url, '/')) - 1:] conn = HTTPConnection(host, timeout=10) conn.request('GET', path) req = conn.getresponse() r_url = req.getheader('Location') conn.close() return r_url
def test_garbage_in(self): # Connect without SSL regardless of server.scheme c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c._output(b'gjkgjklsgjklsgjkljklsg') c._send_output() response = c.response_class(c.sock, method='GET') try: response.begin() self.assertEqual(response.status, 400) self.assertEqual(response.fp.read(22), b'Malformed Request-Line') c.close() except socket.error: e = sys.exc_info()[1] # "Connection reset by peer" is also acceptable. if e.errno != errno.ECONNRESET: raise
def getpage(): host = '%s:%s' % (self.interface(), self.PORT) if self.scheme == 'https': c = HTTPSConnection(host) else: c = HTTPConnection(host) try: c.putrequest('GET', '/') c.endheaders() response = c.getresponse() body = response.read() self.assertEqual(response.status, 200) self.assertEqual(body, b'Hello world!') finally: c.close() next(success)
def request(index): if self.scheme == 'https': c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) else: c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) for i in range(request_count): c.putrequest('GET', '/') for k, v in cookies: c.putheader(k, v) c.endheaders() response = c.getresponse() body = response.read() if response.status != 200 or not body.isdigit(): errors.append((response.status, body)) else: data_dict[index] = max(data_dict[index], int(body))