def takeoff(header, alt): body = json.dumps({"takeoff_alt": alt}) http_client = httpclient.HTTPClient() response = http_client.fetch( "http://dev.flytbase.com/rest/ros/flytsim/navigation/take_off", method='POST', headers=header, body=body) print(response.body) http_client.close() return response.body
def tornado(): http_client = httpclient.HTTPClient() try: response = http_client.fetch( httpclient.HTTPRequest(url="http://localhost:8080/sleep", request_timeout=70)) return response.body except Exception as e: return e else: return 'else from tornado request'
def make_request(self, role, access_key, secret_key, token): url = self.get_url( '/latest/meta-data/iam/security-credentials/{}?role={}&access_' 'key={}&secret_key={}&token={}'.format(role, role, access_key, secret_key, token)) with mock.patch('tornado_aws.config.INSTANCE_ENDPOINT', url): obj = config.Authorization('default', client=httpclient.HTTPClient()) with mock.patch.object(obj, '_get_role') as get_role: get_role.return_value = role return obj._fetch_credentials()
def timeline(for_user): tok = tokens[for_user] http_client = httpclient.HTTPClient() headers = {'Content-Type': 'application/json', 'Authorization': tok} req = httpclient.HTTPRequest("http://localhost:8802/api/timeline", method='GET', headers=headers) res = http_client.fetch(req) res = json.loads(res.body.decode('utf-8')) print("timeline for ", for_user) for t in res['timeline']: print("\t", t['time'], t['text'])
def realize(self, item): wget_args = [ WGET_LUA, '-U', USER_AGENT, '-nv', '--no-cookies', '--content-on-error', '--lua-script', 'playstv.lua', '-o', ItemInterpolation('%(item_dir)s/wget.log'), '--no-check-certificate', '--output-document', ItemInterpolation('%(item_dir)s/wget.tmp'), '--truncate-output', '-e', 'robots=off', '--rotate-dns', '--recursive', '--level=inf', '--no-parent', '--page-requisites', '--timeout', '30', '--tries', 'inf', '--domains', 'plays.tv', '--span-hosts', '--waitretry', '30', '--warc-file', ItemInterpolation('%(item_dir)s/%(warc_file_base)s'), '--warc-header', 'operator: Archive Team', '--warc-header', 'playstv-dld-script-version: ' + VERSION, '--warc-header', ItemInterpolation('playstv-item: %(item_name)s'), ] item_name = item['item_name'] assert ':' in item_name item_type, item_value = item_name.split(':', 1) item['item_type'] = item_type item['item_value'] = item_value http_client = httpclient.HTTPClient() if item_type == 'user': wget_args.extend(['--warc-header', 'playstv-user: '******'https://plays.tv/u/' + item_value) else: raise Exception('Unknown item') http_client.close() if 'bind_address' in globals(): wget_args.extend(['--bind-address', globals()['bind_address']]) print('') print('*** Wget will bind address at {0} ***'.format( globals()['bind_address'])) print('') return realize(wget_args, item)
def sync(): client = httpclient.HTTPClient() try: res = client.fetch('http://httpbin.org/get') print res.body, 'sync' except httpclient.HTTPError as e: print('http error: ' + str(e)) except Exception as e: print('error: ' + str(e)) client.close()
def __init__(self, host): self.http_client = httpClient.HTTPClient() try: response = self.http_client.fetch(host) print(response.body) except httpClient.HTTPError as e: # HTTPError is raised for non-200 responses; the response # can be found in e.response. print("Error: " + str(e)) except Exception as e: # Other errors are possible, such as IOError. print("Error: " + str(e))
def _get_url(self, url, params=None, headers=None): params = {} if params is None else params headers = {} if headers is None else headers my_client = httpclient.HTTPClient() try: resp = my_client.fetch(httpclient.HTTPRequest(url, headers=headers, body=safe_urlencode(params), allow_nonstandard_methods=True)) finally: my_client.close() return force_unicode(resp.body)
def _request_region_from_instance(): """Attempt to get the region from the instance metadata :rtype: str """ url = INSTANCE_ENDPOINT.format(REGION_PATH) response = httpclient.HTTPClient().fetch(url, connect_timeout=HTTP_TIMEOUT, request_timeout=HTTP_TIMEOUT) data = json.loads(response.body.decode('utf-8')) return data['region']
def httppost(url, **kwgs): httpClient = None try: httpClient = httpclient.HTTPClient() httpReq = httpclient.HTTPRequest(url=url, method="POST") httpResp = httpClient.fetch(httpReq) print(httpResp.body) except httpclient.HTTPError as e: print(e) finally: if httpClient != None: httpClient.close()
def _get_schema_fields(self): """ Fetches Solr schema fields using Solr JSON API. Returns: a dict containing schema fields. """ solr_url = '{}/solr/schema/fields'.format(self.solr_location) headers = {'Content-Type': 'application/json'} client = httpclient.HTTPClient() response = client.fetch(solr_url, headers=headers, raise_error=True) json_response = json.loads(response.body.decode('utf-8')) return {field['name']: field for field in json_response['fields']}
def testGenerateHeadUrl(self): """Test GenerateUrl method with 'HEAD' method on S3 object store.""" self._RunAsync(self.object_store.Put, self.key, 'foo') url = self.object_store.GenerateUrl(self.key, method='HEAD', expires_in=100) response = httpclient.HTTPClient().fetch(url, method='HEAD', request_timeout=3.0) self.assertEqual(response.code, 200) self.assertEqual(response.headers['Content-Length'], '3')
def QueryFacebookTestUsers(self, limit): url = (_FACEBOOK_QUERY_TEST_USERS_URL % secrets.GetSecret('facebook_api_key')) + '?' + \ urllib.urlencode({'access_token': self._access_token, 'limit': limit}) http_client = httpclient.HTTPClient() response = http_client.fetch(url, request_timeout=100) try: json_data = json.loads(response.body) return json_data['data'] except: logging.error('unable to query facebook test users: %s' % response.body) raise
def get_nodes(): """gets a list of all routers within the bounding box from openwifimap""" global cache url = "https://api.openwifimap.net/view_nodes_spatial?bbox=" + bounding_box if url in cache: return cache[url] http_client = httpclient.HTTPClient() response = http_client.fetch(url) http_client.close() body = response.body cache.set(url, body, expire=60 * 10) return body
def _load_remote_data(self, url): client = httpclient.HTTPClient() kwargs = { 'method': 'GET', 'validate_cert': self.session.verify, 'client_key': self._get_client_key(), 'client_cert': self._get_client_cert(), 'request_timeout': self.load_timeout } http_req = httpclient.HTTPRequest(url, **kwargs) response = client.fetch(http_req) return response.body
def parseRSS(resp): try: feed = feedparser.parse(resp.body) if entry['datetime'] != '': if parser.parse(entry['datetime']) < parser.parse( feed['items'][0]['published']): print("new") #Send the Yo client = httpclient.HTTPClient() req = httpclient.HTTPRequest("http://api.justyo.co/yoall/", method='POST', body="api_token=" + entry['apikey'] + "&link=" + entry['url']) resp = client.fetch(req) #print(resp) if 'id' in feed['items'][0]: id = feed['items'][0]['id'] elif 'title' in feed['items'][0]: id = feed['items'][0]['title'] date = feed['items'][0]['published'] mysql.execute( "UPDATE feeds SET datetime=%s, lastid=%s WHERE id=%s", date, id, entry['id']) else: if 'id' in feed['items'][0]: id = feed['items'][0]['id'] elif 'title' in feed['items'][0]: id = feed['items'][0]['title'] if entry['lastid'] != id: print("new") #Send the Yo client = httpclient.HTTPClientHTTPClient() req = httpclient.HTTPRequest("http://api.justyo.co/yoall/", method='POST', body="api_token=" + entry['apikey'] + "&link=" + entry['url']) mysql.execute( "UPDATE feeds SET datetime=%s, lastid=%s WHERE id=%s", "", id, entry['id']) except Exception: pass
def run_http_client(self, in_url): h_c = httpclient.HTTPClient() try: ret = h_c.fetch(in_url) except httpclient.HTTPError as e: print('http error:', e) return None except Exception as e: print('exception:', e) return None else: return ret.code, ret.body
def account_verity(): ctx = zmq.Context() socket = ctx.socket(zmq.REP) socket.linger = 0 socket.bind("tcp://127.0.0.1:6666") http_client = httpclient.HTTPClient() response = None while True: msg = socket.recv_json() print msg if msg: try: AppToken = "535942062|7f415b11056cf408b0cd87ceb7020466" if msg['Action'] == 'putGameResult': msg['AppToken'] = AppToken elif msg['Action'] == 'putOrderInfo': msg['AppToken'] = AppToken if msg['auth'] == False: fakemsg = dict() fakemsg['error'] = 0 data = dict() if msg['Action'] == 'putGameResult': data['ResultID'] = 'fakeID' elif msg['Action'] == 'putOrderInfo': data['OrderID'] = 'fakeID' fakemsg['data'] = data response = json.dumps(fakemsg) else: del msg['auth'] if msg['Action'] == 'putOrderInfo': msgj = json.dumps(msg["BetList"]) msg["BetList"] = msgj post_data = msg body = urllib.urlencode(post_data) httpresponse = http_client.fetch("http://sooq.us.to/v1.0/", method='POST', body=body) response = httpresponse.body except httpclient.HTTPError as e: # HTTPError is raised for non-200 responses; the response # can be found in e.response. print("Error: " + str(e)) except Exception as e: # Other errors are possible, such as IOError. print("Error: " + str(e)) print response socket.send(response)
def try_download_url(self, session_path, notebook_path, cm): ''' ''' # if it's a url that we can try to download file and save to # a download location from tornado import httpclient from urllib.parse import urlparse, quote, urlencode from urllib.request import urlopen try: download_base = session_path.rsplit('/', 1)[0] + '/Downloads/' result = urlparse(notebook_path) if not result.scheme: print('not a url', notebook_path) return notebook_path respath, resname = os.path.split(quote(result.path)) download_path = download_base + quote(result.netloc) + respath resfile = os.path.join(download_path, resname) try: cm.get(resfile, content=False) print('file exists', resfile) return resfile except: pass print('fetching', notebook_path, 'to', resfile) http_client = httpclient.HTTPClient() response = http_client.fetch( "%s://%s%s" % (result.scheme, result.netloc, quote(result.path)), method=self.request.method, #body=self.request.body, headers=self.request.headers, ) print('saving notebook', resfile) model = dict(type='notebook', format='json') import json model['content'] = json.loads(response.body) http_client.close() # recursively create directory first dirs = '' for p in [download_base, quote(result.netloc)] + respath.split('/'): if p == '': continue dirs += '/' + p print('create directory', dirs) cm.save({'type': 'directory'}, dirs) # save it cm.save(model, resfile) return resfile except Exception as e: print(e) print('got error', notebook_path) return notebook_path
def is_healthy(self) -> None: signed_request = self.override_prepared_request_parameters(self.endpoints.status_endpoint().prepare_request()) http_client = httpclient.HTTPClient() # this will throw if the instance is really borked or we can't connect or we're not allowed (see # https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-status.html ) response = http_client.fetch(signed_request) status = json.loads(response.body, encoding='utf-8') if status.get('status') == 'healthy' and status.get('role') == 'writer': LOGGER.debug(f'status is healthy: {status}') else: # we'll log in healthcheck raise RuntimeError(f'status is unhealthy: {status}')
def common_post_api(path, params={}, method='POST'): url = COMMON_URL + path http_client = httpclient.HTTPClient() try: request = http_request(url, method=method, body=json.dumps(params)) response = http_client.fetch(request) response = json.loads(response.body.decode()) return response except Exception as e: logging.error('url=%s, error=%s' % (url, e)) raise APIError(errcode=10001, errmsg='公共接口请求失败') finally: http_client.close()
def common_api(path, params={}): url = url_concat(COMMON_URL + path, params) http_client = httpclient.HTTPClient() try: request = http_request(url) response = http_client.fetch(request) response = json.loads(response.body.decode()) return response except Exception as e: logging.error('url=%s, error=%s' % (url, e)) raise APIError(errcode=10001, errmsg='公共接口请求失败') finally: http_client.close()
def make_client(): http_client = httpclient.HTTPClient() try: response = http_client.fetch(url) print(response.body) except httpclient.HTTPError as e: # HTTPError is raised for non-200 responses; the response # can be found in e.response. print("Error: " + str(e)) except Exception as e: # Other errors are possible, such as IOError. print("Error: " + str(e)) http_client.close()
def add_region(name_ger, name_ita): http_client = httpclient.HTTPClient() post_data = {'name_ita': name_ita, 'name_ger': name_ger} body = json.dumps(post_data) headers = {'Content-Type': 'application/json'} req = httpclient.HTTPRequest("http://localhost:8802/api/regions", method='PUT', body=body, headers=headers) res = http_client.fetch(req) res = json.loads(res.body.decode('utf-8')) return res['id']
def post(self): facebook_id = self.get_body_argument('facebookId') http_client = httpclient.HTTPClient() url = 'https://graph.facebook.com/%s' % facebook_id try: response = http_client.fetch(url) assert self.save(simplejson.loads(response.body)) log.info('Person created', arguments=response.body) self.set_status(201) except httpclient.HTTPError, e: log.warn('Facebook request error: %s' % url) self.set_status(500)
def __init__(self, web_app_host, web_app_port, request_timeout=40, retries=3, on_fail_sleep_duration=5): """ Set up various behavior of the attempt to connect to the logging server. Not currently configurable. """ logging.Handler.__init__(self) self.addr = web_app_host + ':' + str(web_app_port) self.request_timeout = request_timeout self.retries = retries self.on_fail_sleep_duration = on_fail_sleep_duration self.http_client = httpclient.HTTPClient()
def get_current_user(self): auth_header = self.request.headers.get('Authorization', '') if len(auth_header.split(' ')) < 2: raise web.HTTPError(401, reason='Unauthorized') bearer = auth_header.split(' ')[1] if auth_header is None or bearer is None: raise web.HTTPError(401, reason='Unauthorized') # Retrieve JWK from server # JWK contains public key that is used for decode JWT token # Only keycloak server know private key and can generate tokens # Before retrieve JWK, it's possible to use openid configuration url : /auth/realms/{realm}/.well-known/openid-configuration # This URL list all endpoints that can be used like the following certs url # For simplicity and to reduce network transfers, we use the certs url directly try: request = httpclient.HTTPRequest( self.application.settings['open_id_certs_url'], method='GET', ) response = httpclient.HTTPClient().fetch(request, raise_error=False) if response.code == 200: jwk = json.loads(response.body.decode('utf-8')) public_key = RSAAlgorithm.from_jwk(json.dumps(jwk['keys'][0])) payload = jwt.decode(bearer, public_key, algorithms='RS256', options={'verify_aud': False}) else: raise ValueError(response.body.decode('utf-8')) httpclient.HTTPClient().close() except jwt.ExpiredSignatureError: raise web.HTTPError(401, reason='Unauthorized') return payload
def realize(self, item): wget_args = [ WGET_LUA, '-U', USER_AGENT, '-nv', '--lua-script', 'sketch-static.lua', '-o', ItemInterpolation('%(item_dir)s/wget.log'), '--no-check-certificate', '--output-document', ItemInterpolation('%(item_dir)s/wget.tmp'), '--truncate-output', '-e', 'robots=off', '--rotate-dns', '--recursive', '--level=inf', '--no-parent', '--page-requisites', '--timeout', '30', '--tries', 'inf', '--domains', 'sketch.sonymobile.com,sketch-cloud-storage.s3.amazonaws.com', '--span-hosts', '--waitretry', '30', '--warc-file', ItemInterpolation('%(item_dir)s/%(warc_file_base)s'), '--warc-header', 'operator: Archive Team', '--warc-header', 'sketch-dld-script-version: ' + VERSION, '--warc-header', ItemInterpolation('sketches-created-on: %(item_value)s') ] item_name = item['item_name'] item_type, item_value = item_name.split(':', 1) item['item_type'] = item_type item['item_value'] = item_value http_client = httpclient.HTTPClient() if item_type == 'sketches' or item_type == 'tests': r = http_client.fetch( 'https://raw.githubusercontent.com/marked/sketch-items/master/' + item_type + "/" + item_value, method='GET') for s in r.body.decode('utf-8', 'ignore').splitlines(): s = s.strip() if len(s) == 0: continue wget_args.append( 'https://storage.sketch.sonymobile.com/feed/{}/image'. format(s)) else: raise Exception('Unknown item') http_client.close() if 'bind_address' in globals(): wget_args.extend(['--bind-address', globals()['bind_address']]) print('') print('*** Wget will bind address at {0} ***'.format( globals()['bind_address'])) print('') return realize(wget_args, item)
def get(self): # code provided by github code = self.get_argument('code') post_args = { 'code': code, 'client_id': config.gh_id, 'client_secret': config.gh_secret } # request some shit from github http_client = httpclient.HTTPClient() try: response = json.loads( http_client.fetch(config.gh_ex_url, method='POST', body=urllib.parse.urlencode(post_args), headers={ 'Accept': 'application/json' }).body.decode('utf-8')) access_token = response['access_token'] user = github.get_user(access_token) # add to db if 'id' in user: users.update_user(user['id'], user['login'], access_token, user['avatar_url'], github.get_languages(access_token), user['updated_at']) self.set_secure_cookie('user', str(user['id'])) fetched_user = users.get_user(user['id']) print(fetched_user) self.redirect('/#/match') else: self.redirect('/#/error/login') except httpclient.HTTPError as e: # HTTPError is raised for non-200 responses; the response # can be found in e.response. print('Error', e) print(traceback.format_exc()) except Exception as e: # Other errors are possible, such as IOError. print('Error', e) print(traceback.format_exc())
def test_when_fetch_with_params_then_calls_http_client_fetch_with_params_added_to_url( self): with Spy(httpclient.HTTPClient()) as http_client: session = Session(http_client) session.fetch('/users?type=json', callback=CALLBACK, params={'is_admin': 'true'}) assert_that( http_client.fetch, called().with_args( has_properties(url='/users?type=json&is_admin=true'), callback=CALLBACK))