def auth(username, apikey, servicename=None, region=None, debug=False): """Auth! This may be changed over to httplib in the near future.""" account_data = {} payload = {"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": username, "apiKey": apikey}}} headers = {"content-type": "application/json"} response = requests.post( "https://identity.api.rackspacecloud.com/v2.0/tokens", data=json.dumps(payload), headers=headers ) auth_data = response.json["access"] if not servicename: raise Exception("servicename must be defined") if debug: for i in auth_data["serviceCatalog"]: print i else: for i in auth_data["serviceCatalog"]: if i["name"] == servicename: if region: for serv in i["endpoints"]: if region.upper() == serv["region"]: account_data["uri"] = serv["publicURL"] else: account_data["uri"] = i["endpoints"][0]["publicURL"] account_data["token"] = auth_data["token"]["id"] account_data["id"] = auth_data["token"]["tenant"]["id"] return account_data
def test_POST_RAW_DATA(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value = "value_" + uuid.uuid4().get_hex() data = "%s:%s" % (random_key, random_value) r = requests.post(build_url('post'), data=data) self.assertEquals(r.status_code, 200) self.assertTrue(data in r.content)
def test_POST_RAW_DATA(self): random_key = "key_" + uuid.uuid4().hex[:10] random_value = "value_" + uuid.uuid4().hex data = "%s:%s" % (random_key, random_value) r = requests.post(build_url('post'), data=data) self.assertEquals(r.status_code, 200) self.assertTrue(r.text.find(data) != -1)
def test_POST_RAW_DATA(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value = "value_" + uuid.uuid4().get_hex() data = "%s:%s" % (random_key, random_value) r = requests.post(build_url('post'), data=data) self.assertEquals(r.status_code, 201) self.assertTrue(data in r.content)
def test_FILES(self): files = {'test_file': io.open('test_human_curl.py'), 'test_file2': io.open('README.rst')} r = requests.post(build_url('post'), files=files) json_response = json.loads(r.text) self.assertEquals(r.status_code, 200) for k, v in list(files.items()): self.assertTrue(k in list(json_response['files'].keys()))
def test_FILES(self): files = {'test_file': open('tests.py'), 'test_file2': open('README.rst')} r = requests.post(build_url('post'), files=files) json_response = json.loads(r.content) self.assertEquals(r.status_code, 201) for k, v in files.items(): self.assertTrue(k in json_response['files'].keys())
def test_multivalue_post_data(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value1 = "value_" + uuid.uuid4().get_hex() random_value2 = "value_" + uuid.uuid4().get_hex() r = requests.post(build_url("post"), data={random_key: (random_value1, random_value2)}) json_response = json.loads(r.content) self.assertListEqual([random_value1, random_value2], json_response['form'][random_key])
def test_multivalue_post_data(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value1 = "value_" + uuid.uuid4().get_hex() random_value2 = "value_" + uuid.uuid4().get_hex() r = requests.post(build_url("post"), data={random_key: (random_value1, random_value2)}) json_response = json.loads(r.content) self.assertTrue(random_value1 in json_response['args'][random_key]) self.assertTrue(random_value2 in json_response['args'][random_key])
def test_multipart_post_data(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value = "value_" + uuid.uuid4().get_hex() r = requests.post(build_url("post"), data={random_key: random_value}, headers={'Content-Type': 'multipart/form-data'}) json_response = json.loads(r.content) content_type = json_response['headers']['Content-Type'] self.assertEquals(random_value, json_response['form'][random_key]) self.assertTrue('multipart/form-data; boundary=-' in content_type)
def test_multipart_post_data(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value = "value_" + uuid.uuid4().get_hex() r = requests.post( build_url("post"), data={random_key: random_value}, headers={'Content-Type': 'multipart/form-data'}) json_response = json.loads(r.content) content_type = json_response['headers']['Content-Type'] self.assertEquals(random_value, json_response['form'][random_key]) self.assertTrue('multipart/form-data; boundary=-' in content_type)
def test_multivalue_post_data(self): random_key = "key_" + uuid.uuid4().hex[:10] random_value1 = "value_" + uuid.uuid4().hex random_value2 = "value_" + uuid.uuid4().hex r = requests.post( build_url("post"), data={random_key: (random_value1, random_value2)}) json_response = json.loads(r.text) # print(json_response) self.assertTrue(random_value1 in json_response['form'][random_key]) self.assertTrue(random_value2 in json_response['form'][random_key])
def test_POST_DATA_and_FILES(self): files = {'test_file': open('tests.py'), 'test_file2': open('README.rst')} random_key1 = "key_" + uuid.uuid4().get_hex()[:10] random_value1 = "value_" + uuid.uuid4().get_hex() random_key2 = "key_" + uuid.uuid4().get_hex()[:10] random_value2 = "value_" + uuid.uuid4().get_hex() r = requests.post(build_url('post'), data={random_key1: random_value2, random_key2: random_value2}, files=files) self.assertEquals(r.status_code, 201)
def auth(username, apikey): """Auth! This may be changed over to httplib in the near future.""" payload = {"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": username, "apiKey": apikey}}} headers = {"content-type": "application/json"} response = requests.post( "https://auth.api.rackspacecloud.com/v2.0/tokens", data=json.dumps(payload), headers=headers ) auth_data = response.json["access"] for i in auth_data["serviceCatalog"]: if i["name"] == "cloudMonitoring": endpoint = i["endpoints"][0]["publicURL"] account_data["token"] = auth_data["token"]["id"] account_data["monuri"] = endpoint
def fuzz(url): r = requests.post(url, data=payload, headers={'Content-Type': 'application/json'}) time.sleep(5) c = CloudEye(randoms="") a = c.getRandomDomain(cues) if c.verifyDNS(delay=0): print "success" else: print "failure" #fuzz("http://127.0.0.1:8080/fastjson-1.0/")
def send(self, registration_id, data): headers = { 'Authorization': 'Bearer {0}'.format(self.auth_token), 'Content-Type': 'application/json', 'X-Amzn-Type-Version': '[email protected]', 'Accept': 'application/json', 'X-Amzn-Accept-Type': '[email protected]' } body = json.dumps({'data': data}) result = collections.defaultdict(dict) try: response = requests.post(self.url_message.format(registration_id), body, headers=headers, proxy=self.proxy) except exceptions.CurlError: raise ADMConnectionException( "Internal error in ADM server or in the network") if response.status_code == 200: canonical = json.loads(response.content).get('registrationID') if canonical is not None and canonical != registration_id: result['canonical'][registration_id] = canonical elif response.status_code == 400: reason = json.loads(response.content).get('reason') if reason is not None and reason == 'InvalidRegistrationId': result['errors'][registration_id] = reason else: raise ADMException("Bad Request: {0}", response.content) elif response.status_code == 401: #internal auth token management fails, ensure next call recalculate it self._auth_token = None raise ADMException("Unable to refresh token: {0}", response.content) elif response.status_code == 413: raise ADMException( "Maximum allowable data size (6 KB) reached: {0}", response.content) elif response.status_code == 429: raise ADMException("Maximum allowable rate reached: {0}", response.content) else: raise ADMException("Internal Server error: {0}{1}".format( response.status_code, response.content)) return result
def auth_token(self): # token is cached until it expires if self._auth_token is not None and time.time( ) - self._auth_expire < self._auth_timestamp: return self._auth_token headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Charset': 'UTF-8', } body = urllib.urlencode({ 'grant_type': 'client_credentials', 'scope': 'messaging:push', 'client_id': self.client_id, 'client_secret': self.client_secret }) try: response = requests.post(self.url_auth, body, headers=headers, proxy=self.proxy) except exceptions.CurlError: raise ADMConnectionException( "Internal error in ADM server or in the network") if response.status_code == 400: raise ADMBadRequestException("{0}:{1}".format( response.status_code, response.content)) elif response.status_code == 401: raise ADMAuthenticationException("{0}:{1}".format( response.status_code, response.content)) elif response.status_code != 200: raise ADMException("{0}:{1}".format(response.status_code, response.content)) data = json.loads(response.content) # !! Using human_curl through http proxy is not closing the socket properly, # the performance will decrease due the number of connections opened. # With explicit close the socket will remain opened for TIME_WAIT seconds until system timeout will expire it. response._curl_opener.close() self._auth_expire = 90 * data[ 'expires_in'] / 100 # ensure we refresh token before expiring self._auth_timestamp = time.time() token = data['access_token'] self._auth_token = token return token
def sendResponse(self, batch_id, results): global _AJAXURL, _AUTH opts = self.options url = _AJAXURL % { 'action': 'put_ar_data'} data = { 'object' : json.dumps({ 'id': batch_id, 'object': results })} while True: try: resp = requests.post(url, data, timeout=10, auth=_AUTH) break; except requests.exceptions.CurlError as ex: print 'ERROR ', ex.message, ' Retrying...' #except requests.exceptions.Timeout: # print 'Send request timed out. Retrying...' if resp.status_code != 200 or resp.content == False: print 'ERROR: status_code %d or no content' % resp.status_code return False obj = json.loads(resp.content); if obj == False: print 'ERROR: content parse error' print resp.content return False if obj['state'] != 'OK': print 'ERROR: ', obj['message'] return False if obj['result'] == 'TMOUT': return False if obj['result'] == 'PASS': return True print 'Got unknown result: ', obj return False
def send(self, registration_id, data): headers = { 'Authorization': 'Bearer {0}'.format(self.auth_token), 'Content-Type': 'application/json', 'X-Amzn-Type-Version': '[email protected]', 'Accept': 'application/json', 'X-Amzn-Accept-Type': '[email protected]' } body = json.dumps({'data': flatten(data)}) result = collections.defaultdict(dict) try: response = requests.post(self.url_message.format(registration_id), body, headers=headers, proxy=self.proxy) except exceptions.CurlError: raise ADMConnectionException("Internal error in ADM server or in the network") if response.status_code == 200: canonical = json.loads(response.content).get('registrationID') if canonical is not None and canonical != registration_id: result['canonical'][registration_id] = canonical elif response.status_code == 400: reason = json.loads(response.content).get('reason') if reason is not None and reason == 'InvalidRegistrationId': result['errors'][registration_id] = reason else: raise ADMException("Bad Request: {0}", response.content) elif response.status_code == 401: #internal auth token management fails, ensure next call recalculate it self._auth_token = None raise ADMException("Unable to refresh token: {0}", response.content) elif response.status_code == 413: raise ADMException("Maximum allowable data size (6 KB) reached: {0}", response.content) elif response.status_code == 429: raise ADMException("Maximum allowable rate reached: {0}", response.content) else: raise ADMException("Internal Server error: {0}{1}".format(response.status_code, response.content)) return result
def auth_token(self): # token is cached until it expires if self._auth_token is not None and time.time() - self._auth_expire < self._auth_timestamp: return self._auth_token headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Charset': 'UTF-8', } body = urllib.urlencode({'grant_type': 'client_credentials', 'scope': 'messaging:push', 'client_id': self.client_id, 'client_secret': self.client_secret}) try: response = requests.post(self.url_auth, body, headers=headers, proxy=self.proxy) except exceptions.CurlError: raise ADMConnectionException("Internal error in ADM server or in the network") if response.status_code == 400: raise ADMBadRequestException("{0}:{1}".format(response.status_code, response.content)) elif response.status_code == 401: raise ADMAuthenticationException("{0}:{1}".format(response.status_code, response.content)) elif response.status_code != 200: raise ADMException("{0}:{1}".format(response.status_code, response.content)) data = json.loads(response.content) # !! Using human_curl throught http proxy is not closing the socket properly, # the performance will decrease due the number of coonections opened. # With the explicit close the socket will remain TIME_WAIT until system timeout will expire it. response._curl_opener.close() self._auth_expire = 90 * data['expires_in'] / 100 # ensure we refresh token before expiring self._auth_timestamp = time.time() token = data['access_token'] self._auth_token = token return token
prettify(projects) print print 'Components will be combined with labels as github labels...' proj = raw_input('Project to use: ') us = raw_input('Repo User: '******'Repo to use: ') user = raw_input('User: '******'Pass: '******'https://api.github.com/repos/' + us + '/' + repo print 'Making milestones...', url_frag + '/milestones' print for mkey in projects[proj]['Milestones'].iterkeys(): data = {'title': mkey} r = hurl.post(url_frag + '/milestones', json.dumps(data), auth=(user, pw)) # overwrite histogram data with the actual milestone id now if r.status_code == 201: content = json.loads(r.content) projects[proj]['Milestones'][mkey] = content['number'] print mkey else: if r.status_code == 422: # already exists ms = json.loads( hurl.get(url_frag + '/milestones?state=open').content) ms += json.loads( hurl.get(url_frag + '/milestones?state=closed').content) f = False for m in ms: if m['title'] == mkey: projects[proj]['Milestones'][mkey] = m['number']
<?xml version=\"1.0\" encoding=\"utf-8\"?> <message> <param> <name>messageType</name> <value>SMS</value> </param> <param> <name>id</name> <value>319</value> </param> <param> <name>source</name> <value>09179860039</value> </param> <param> <name>target</name> <value>23737390</value> </param> <param> <name>msg</name> <value>%s</value> </param> <param> <name>udh</name> <value></value> </param> </message>""" % msg r = requests.post(url, data=content) print r.status_code
def test_POST_DATA(self): random_key = "key_" + uuid.uuid4().get_hex()[:10] random_value = "value_" + uuid.uuid4().get_hex() r = requests.post(build_url('post'), data={random_key: random_value}) self.assertEquals(r.status_code, 201)
with timer("python-requests"): r = requests.get('http://httpbin.org/redirect/7', allow_redirects=True) print(r) print(len(r.history)) files = { #('first_file', open("/tmp/testfile1.txt.gz")), 'first_file': open("/tmp/testfile2.txt"), 'second_file': open("/tmp/testfile3.txt"), } #FILES UPLOADING with timer("human_curl"): try: r = human_curl.post('http://h.wrttn.me/post', allow_redirects=True, files=files, max_redirects=10) print(r) #print(json.loads(r.content)) except Exception, e: print(e) with timer("python-requests"): try: r = requests.post('http://h.wrttn.me/post', allow_redirects=True, files=files) print(r) #print(json.loads(r.response)) except Exception, e: print(e)
def test_HTTP_POST(self): r = requests.post(build_url("post")) self.assertEquals(r.status_code, 201)
def post(uri, data): headers = {"X-Auth-Token": account_data["token"], "content-type": "application/json"} return requests.post(uri, data=data, headers=headers)
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import datetime from base64 import b64encode import human_curl as hurl metric_data = { "name": "my_metric_name", "timestamp": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S"), "filters": { "views": "registered", "clicks": "anonymouse" }, "action": "incr", "value": 2 } headers = (("content-type", "application/json"), ("Authorization", b64encode("{0}:{1}".format("public key", "dwefwefwefwecwef")))) r = hurl.post("http://127.0.0.1:8889/test_project/api/store", data=json.dumps(metric_data), headers=headers) print(r.content)
with timer("python-requests"): r = requests.get('http://httpbin.org/redirect/7', allow_redirects=True) print(r) print(len(r.history)) files = { #('first_file', io.open("/tmp/testfile1.txt.gz")), 'first_file': io.open("/tmp/testfile2.txt"), 'second_file': io.open("/tmp/testfile3.txt"), } #FILES UPLOADING with timer("human_curl"): try: r = human_curl.post('http://h.wrttn.me/post', allow_redirects=True, files=files, max_redirects=10) print(r) #print(json.loads(r.content)) except Exception, e: print(e) with timer("python-requests"): try: r = requests.post('http://h.wrttn.me/post', allow_redirects=True, files=files) print(r) #print(json.loads(r.response)) except Exception, e: print(e) custom_headers = ( ('Test-Header', 'fwkjenwkljbnfkjqnewfrjven3lrf'),
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import datetime from base64 import b64encode import human_curl as hurl metric_data = {"name": "my_metric_name", "timestamp": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S"), "filters": {"views": "registered", "clicks": "anonymouse"}, "action": "incr", "value": 2} headers = (("content-type", "application/json"), ("Authorization", b64encode("{0}:{1}".format("public key", "dwefwefwefwecwef")))) r = hurl.post("http://127.0.0.1:8889/test_project/api/store", data=json.dumps(metric_data), headers=headers) print r.content
def post(uri, data, token=None): headers = {"X-Auth-Token": token, "content-type": "application/json"} return requests.post(uri, data=json.dumps(data), headers=headers)