def pull_data_from_scope(scope, session): print('Retrieving data from gearsheet..') params = urllib.parse.urlencode({'scope': scope, 'format': 'verbose'}) url = 'https://script.google.com/macros/s/AKfycbwQY10fvbOH0eo3TQ6X-uYe_TfLcWanIdqMKBx7EiXz67Uiem0/exec?%s' \ % params response = urllib.request.urlopen(url).read().decode('utf-8') print('Data successfully retrieved.') data = json.loads(response) client = http.client.HTTPConnection("localhost:9000") for item in data: # print(item) item['name'] = item['name'].strip() item_json = json.dumps(item) client.request('POST', '/document/%s' % (scope), item_json, {'Content-Type': 'application/json', 'X-BB-SESSION': session}) item_add_response = json.loads(client.getresponse().read().decode('utf-8')) if result_is_ok(item_add_response): print('successfully added an item', item['name']) item_id = item_add_response['data']['id'] # add an index for the talent print('indexing...') index_json = json.dumps({'name': item['name'].lower(), 'collection': scope, 'item_id': item_id}) client.request('POST', '/document/indexes', index_json, {'Content-Type': 'application/json', 'X-BB-SESSION': session}) index_add_response = json.loads(client.getresponse().read().decode('utf-8')) if result_is_ok(index_add_response): print("indexing %s successful" % scope)
def get_courses(url, campus, dept_link): """Gets courses from a department's course description page. Args: url: The base URL for course descriptions. campus: The name of the department's campus. dept_link: A link to the department's course description page. Returns: A list of courses offered by the department. """ client = http.client.HTTPConnection(url.netloc) client.request('GET', '%s%s' % (url.path, dept_link)) response = client.getresponse() if response.status != 200: logging.warning('Error reading category (%s): %d %s', dept_link, response.status, response.read()) return tree = lxml.html.fromstring(response.read()) client.close() items = tree.xpath('/html/body/a/p') courses = [] for i in items: course = parse_course(i, campus) if not course: logging.warning('Unable to parse course: %s', lxml.html.tostring(i)) continue courses.append(course) return courses
def main(): # init baasbox print('Logging in to backend api...') # scopes = ['weaponmods'] scopes = ['weapontalents', 'playertalents', 'geartalents', 'gearsets', 'weapons', 'weaponmods', 'exoticgears', 'gearattributes'] login_params = json.dumps({'username': '******', 'password': '******', 'appcode': 'gearsheet'}) client = http.client.HTTPConnection("localhost:9000") client.request('POST', '/login', login_params, {'Content-Type': 'application/json'}) login_response = client.getresponse() login_response = json.loads(login_response.read().decode('utf-8')) if not result_is_ok(login_response): print("Login to baasbox failed") return session = login_response['data']["X-BB-SESSION"] print('Login successful.') # reset the db print('resetting the db...') reset_params = json.dumps(scopes + ['indexes']) client.request('POST', '/plugin/bot.index', reset_params, headers={'Content-Type': 'application/json', 'X-BB-SESSION': session}) reset_response = json.loads(client.getresponse().read().decode('utf-8')) print('db reset done') for scope in scopes: pull_data_from_scope(scope, session)
def assertHttpContent(self, route: str): page_name = route.split("/")[-1] print("Connecting to " + self.host + ':' + self.port + '/' + route.lstrip("/")) client = http.client.HTTPConnection(self.host + ':' + self.port) client.request("GET", route) response = client.getresponse() print(response.status, response.reason) self.assertEqual(response.status, 200, "Should have found page " + self.host + ':' + self.port + '/' + route.lstrip("/")) contents = response.read().decode('utf-8') client.close() self.assertIsNotNone(contents) self.assertTrue(page_name in contents) self.assertTrue('POM_MODEL_VERSION=4.0.0' in contents) self.assertTrue('POM_GROUP_ID=org.example' in contents) self.assertTrue('POM_ARTIFACT_ID=mkdocs-pom-parser-plugin' in contents) self.assertTrue('POM_PACKAGING=jar' in contents) self.assertTrue('POM_VERSION=1.0.0-SNAPSHOT' in contents) self.assertTrue('POM_NAME=mkdocs-pom-parser-plugin name' in contents) self.assertTrue('POM_DESCRIPTION=Some description' in contents) self.assertTrue('POM_URL=https://github.com' in contents) self.assertTrue('POM_SCM_CONNECTION=scm:git:git://github.com' in contents) self.assertTrue(escape('<groupId>org.example</groupId>') in contents) self.assertTrue(escape('<artifactId>mkdocs-pom-parser-plugin</artifactId>') in contents) self.assertTrue(escape('<version>1.0.0-SNAPSHOT</version>') in contents)
def get_department_links(url): """Gets department links from the course index page. Args: url: The URL of the index page containing a list of departments. Returns: A set of department links found on the page. Raises: Exception: If an error occurred fetching the list of department links. """ client = http.client.HTTPConnection(url.netloc) client.request('GET', url.path) response = client.getresponse() if response.status != 200: raise Exception('Error reading index: %d %s' % (response.status, response.read())) tree = lxml.html.fromstring(response.read()) client.close() depts = tree.xpath( '/html/body/*/*/*/*/div[contains(@class, "uw-content")]//li/a') return set([i.get('href') for i in depts])
def get(self): if BridgeConfigHandler.bridges == None: client = http.client.HTTPConnection( manager.config['lampdest'], manager.config['lampport'] ) print("BridgeConfig GET /bridges") client.request("GET", "/bridges"); json = client.getresponse().read().decode() try: response = tornado.escape.json_decode(json) except ValueError: print("ValueError: Did not get json from server when requesting /bridges") print(json) self.write("<p>Did not get json from server. Is the IP and port correct? Check the output in console</p>") else: if response['state'] == 'success': data = response['bridges'] print("BridgeConfig response:", data) BridgeConfigHandler.bridges = data self.render('config_bridges.html', bridges=data) else: self.write("<p>Unexpected answer from lamp-server.</p>") self.write("<p>" + str(response) + "</p>") self.write("<p>Expected 'state':'success'</p>") else: self.render('config_bridges.html', bridges=BridgeConfigHandler.bridges)
def almacenarProyectoTM(nombre, areaInteres): headers = { 'Authorization': settings['tm-token'], 'Accept-Language': 'en', 'Content-Type': 'application/json; charset=UTF-8' } info = { "areaOfInterest": { "type": "FeatureCollection", "features": [areaInteres] }, "projectName": nombre, "arbitraryTasks": True } client = http.client.HTTPConnection(settings['tm'], int(settings['tm-puerto']), timeout=int( settings['timeout-request'])) client.request('PUT', '/api/v1/admin/project', json.dumps(info), headers) response = client.getresponse() if response.status == 201: return json.loads(response.read())['projectId'] else: return False
def __send_post_request__(self, db, query): """This method sends a post request with the appropriate query for ALTER CREATE DELETE DROP GRANT KILL REVOKE! """ self.log.info('Start post request: db=' + db + ' q=' + query) client = self.__conn_setup__() try: client.request("POST", "/query?db=" + db + "&q=" + query, headers=self.__get_header__()) resp = client.getresponse() self.log.info('Post response is: ' + str(resp.status)) if resp.status >= 400: raise Exception iserror = str(resp.read().decode('utf-8')) self.log.debug(iserror) if iserror.find('error') >= 0: raise Exception return resp.status except json.JSONDecodeError: self.log.error( 'No JSON came back from the get request to influx DB') return 400 except Exception as e: self.log.error('The POST Request did not succeed, Status: ', e) return 400
def tweet(self, message): authorization_params = { "oauth_consumer_key": self.consumer_key, "oauth_nonce": self.oauth_nonce, "oauth_signature": self.oauth_sign(message), "oauth_signature_method": self.oauth_signature_method, "oauth_timestamp": self.oauth_timestamp, "oauth_token": self.access_token, "oauth_version": self.oauth_version, } oauth_header = ", ".join([ f'{p}="{encode(authorization_params[p])}"' for p in sorted(authorization_params.keys()) ]) client = http.client.HTTPSConnection("api.twitter.com") client.request( "POST", "/1.1/statuses/update.json", body=f"status={encode(message)}", headers={ "Authorization": f"OAuth {oauth_header}", "Content-Type": "application/x-www-form-urlencoded", }, ) response = client.getresponse() print(response.status, response.reason) data = response.read() print(data) client.close()
def single_page(course, quarter): while (1): try: client = http.client.HTTPSConnection(host) client.connect() ''' client.request("GET",url) res = client.getresponse() ''' ############################ body[quarter_key] = quarter body[course_key] = course #print(body) data = urllib.parse.urlencode(body) client.request("POST", url, data, headers) res = client.getresponse() #print(res.getheaders()) raw_codes = res.read() html = gzip.decompress(raw_codes) html = html.decode("utf-8") ################################## filepath = "./raw_data/" + course + "_" + quarter + ".html" f = open(filepath, "w+") f.write(html) f.close() client.close() time.sleep(0.1) break ############################### except: print(raw_codes) print("error when reading this page!!!skipping!") break
def play(room_id, token, server, debug=False, alias=''): """ Connect to game server and play rounds in the loop until end of game. """ # connect to the game server client = http.client.HTTPConnection(server) client.connect() game_url = '/games/{}/board?token={}'.format(room_id, token) if alias: game_url += '&alias={}'.format(alias) # wait until the game starts client.request('GET', game_url) response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) #debug = True if debug: #print(data) pp=pprint.PrettyPrinter(indent=4) pp.pprint(data) # sleep 3 seconds so, you will be able to read printed data time.sleep(3) # make your move and wait for a new round client.request('POST', game_url, json.dumps(get_move(data))) response = client.getresponse()
def do_server_play(token, game, strategy): client = http.client.HTTPConnection(SERVER, 8080) client.connect() # block until the game starts client.request('GET', '/games/{}/board?token={}'.format(game, token)) i = 0 response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) print("[***] step {}, current score {}, moves we have left {}".format( i, data["score"], data["moves"])) next_cursor = solve( data["board"], strategy, data["score"] ) time.sleep(0.2) # make move and wait for a new round client.request( 'POST', '/games/{}/board?token={}'.format(game, token), json.dumps({ 'x': next_cursor.x, 'y': next_cursor.y, }) ) response = client.getresponse() i += 1 else: print("[i] response done, got server code {}".format(response.status))
def getresults(query): domain = "www.trainchinese.com" urlTemmplate = "/v1/a_user/searchForWords.php?searchWord={}" url = urlTemmplate.format(urllib.parse.quote(query)) client = http.client.HTTPConnection(domain) client.request("GET", url) response = client.getresponse() responseText = response.read().decode() regex = re.compile(r'sr_oneWord\((.+?)\)\+') matches = re.findall(regex, responseText) spanRegex = re.compile(r'<\/?span[^>]*>') results = [] for match in matches: match = match.replace("'", "\"") match = "[" + match + "]" result = json.loads(match) name = result[2] name = re.sub(spanRegex, "", name) name = html.parser.HTMLParser().unescape(name) audio = "/v1/" + result[6][3:] results.append([name, audio]) return results
def do_POST(self): try: post_json = json.loads( self.rfile.read(int(self.headers.get('Content-Length', 0))).decode()) result = check_json(post_json) if result[0] is False: self.set_headers() self.construct_json(response=None, timeout=False, invalid_json=True) else: client = http.client.HTTPConnection(result[2][0], timeout=result[5]) try: client.request(result[1], result[2][1], headers=result[3], body=result[4]) self.set_headers() self.construct_json(client.getresponse(), False) except socket.timeout: self.set_headers() self.construct_json(None, True) except ValueError: self.set_headers() self.construct_json(None, False, True)
def cerrarChangeset(changeset): client = http.client.HTTPSConnection(osmRestApiUrl) client.request('PUT', '/api/0.6/changeset/' + changeset + '/close', None, osmHeaders()) response = client.getresponse()
def mymsfConsoleRead(myid): jobs[myid]='<started>' listout=[] msfToken() msfConsole() #results? client = http.client.HTTPConnection('localhost',55552) params=msgpack.packb(['console.read',msftoken,msfconsoleid]) client.request("POST","/api/",params, msfheaders) response = client.getresponse() if response.status == 200: resdata=msgpack.unpackb(response.read()) if len(resdata[b'data'])>0: for line in bytes.decode(resdata[b'data']).split('\n'): dictout={} #remove the msf > prompt and spaces since we've got limited space #and remove any \x01\x02 type chars in the prompt since it errors python xmlrpc. prompt=safestring(bytes.decode(resdata[b'prompt'])) prompt=prompt.replace('msf > ','') prompt=prompt.replace('msf','') dictout['prompt']=prompt.strip() dictout['line']=safestring(line) listout.append(dictout) jobs[myid]=listout
def close_trade(self, trade_id, amount=None): """ Close existing market trade Return values: 204 Ok. No content. 400 Bad Request. The request could not be understood by the server due to malformed syntax. 401 Unauthorized. The request requires user authentication. 402 Payment Required. Not enough money for the operation. 403 Forbidden. The request is forbidden due to limited access rights. 404 Not Found. Required trade was not found. 410 Gone. Off quotes or dealer reject. 500 Internal Server Error. The server encountered an unexpected condition which prevented it from fulfilling the request. Keyword arguments: trade_id -- trade Id to close amount -- amount to close (optional) """ client = self.__create_http_client() method = 'DELETE' if amount is None: url_relative = '/api/v1/trade?type=Close&id={0}'.format(trade_id) else: url_relative = '/api/v1/trade?type=Close&id={0}&amount={1}'.format(trade_id, amount) url_absolute = 'https://{0}{1}'.format(self.__web_api_address, url_relative) client.request(method, url_relative, None, self.__get_http_hmac_headers(method, url_absolute, None)) return client.getresponse().status
def do_POST(self): length = int(self.headers.get('content-length', 0)) data = self.rfile.read(length) try: loaded_json = json.loads(data) except ValueError: self.send_json({"code": "invalid json"}) return if "url" not in loaded_json.keys() or ( "type" in loaded_json.keys() and "content" not in loaded_json.keys() and loaded_json["type"] == "POST"): self.send_json({"code": "invalid json"}) return timeout = loaded_json["timeout"] if "timeout" in loaded_json.keys( ) else 1 body = loaded_json["content"] if "type" in loaded_json.keys( ) and loaded_json["type"] == "POST" else None if body is not None and not isinstance(body, str): body = json.dumps(body) headers = loaded_json["headers"] if "headers" in loaded_json.keys( ) else dict() parsed_url = url_parse(loaded_json["url"]) client = http.client.HTTPConnection(parsed_url[0], timeout=timeout) try: client.request(loaded_json["type"], parsed_url[1], body, headers) except socket.timeout: self.send_json({"code": "timeout"}) else: self.create_json(client.getresponse())
def create_trade(self, trade): """ Create new trade New trade request is described by the filling following fields: - ClientId (optional) - Client trade Id - Type (required) - Type of trade. Possible values: "Market", "Limit", "Stop" - Side (required) - Side of trade. Possible values: "Buy", "Sell" - Symbol (required) - Trade symbol (e.g. "EURUSD") - Price (optional) - Price of the "Limit" / "Stop" trades (for Market trades price field is ignored) - Amount (required) - Trade amount - StopLoss (optional) - Stop loss price - TakeProfit (optional) - Take profit price - ExpiredTimestamp (optional) - Expiration date and time for pending trades ("Limit", "Stop") - ImmediateOrCancel (optional) - "Immediate or cancel" flag (works only for "Limit" trades) - Comment (optional) - Client comment Keyword arguments: trade -- create trade request """ client = self.__create_http_client() method = 'POST' url_relative = '/api/v1/trade' url_absolute = 'https://{0}{1}'.format(self.__web_api_address, url_relative) body = json.dumps(trade) client.request(method, url_relative, body.encode(), self.__get_http_hmac_headers(method, url_absolute, body)) response = json.loads(self.__decode_response(client.getresponse())) return response
def __init__(self): try: config = JsonHandler().LoadJson('Config.json') print(config) Client = pymongo.MongoClient("mongodb://localhost:27017/") dataBase = Client['DBB_db'] PostCollection = dataBase['PostCollection'] postList = [] cursor = PostCollection.find({}) for doc in cursor: postList.append(doc) sortedList = sorted(postList, key=lambda i: i['PayLoadToken'], reverse=True) print(str(len(sortedList))) token = sortedList[0]['PayLoadToken'] if len(sortedList) > 0 else 0 payload = {"PayLoadToken": token} client = http.client.HTTPConnection(config['CordinatorHost'], config['CordinatorHTTPPort']) client.request('POST', "/recovery", json.dumps(payload), {'Content-type': 'application/json'}) response = client.getresponse() if response.status == 200: responseData = response.read() responseJSon = json.loads(responseData) if len(responseJSon) > 0: PostCollection.insert_many(responseJSon) except Exception as ex: print(ex)
def get_daily_snapshots(self, request): """ Get daily account snapshots New daily account snapshots request is described by the filling following fields: - **TimestampFrom** (optional) - Lower timestamp bound of the daily account snapshot request - **TimestampTo** (optional) - Upper timestamp bound of the daily account snapshot request - **RequestDirection** (optional) - Request paging direction ("Forward" or "Backward"). Default is "Forward". - **RequestPageSize** (optional) - Request page size. Default is 100. - **RequestFromId** (optional) - Request paging from Id If timestamps fields are not set daily account snapshot will be requests from the begin or from the current timestamp depending on **RequestDirection** value. Snapshots are returned by chunks by paging size (default is 100). You can provide timestamp bounds (from, to) and direction of access (forward or backward). After the first request you'll get a list of daily account snapshots records with Ids. The next request should contain **RequestFromId** with the Id of the last processed snapshot record. As the result you'll get the next chunk of daily account snapshots records. If the last page was reached response flag **IsLastReport** will be set. Keyword arguments: request -- daily account snapshots request """ client = self.__create_http_client() method = 'POST' url_relative = '/api/v1/dailysnapshots' url_absolute = 'https://{0}{1}'.format(self.__web_api_address, url_relative) body = json.dumps(request) client.request(method, url_relative, body.encode(), self.__get_http_hmac_headers(method, url_absolute, body)) response = json.loads(self.__decode_response(client.getresponse())) return response
def getresults(query): domain = "www.trainchinese.com" urlTemmplate = "/v1/a_user/searchForWords.php?searchWord={}" url = urlTemmplate.format(urllib.parse.quote(query)) client = http.client.HTTPConnection(domain) client.request("GET", url) response = client.getresponse() responseText = response.read().decode(); regex = re.compile(r'sr_oneWord\((.+?)\)\+') matches = re.findall(regex, responseText) spanRegex = re.compile(r'<\/?span[^>]*>') results = [] for match in matches: match = match.replace("'", "\"") match = "[" + match + "]" result = json.loads(match) name = result[2] name = re.sub(spanRegex, "", name) name = html.parser.HTMLParser().unescape(name) audio = "/v1/" +result[6][3:] results.append([name, audio]) return results
def createcustomer(client, firstname, lastname, streetnum, streetname, city, state, zipcode): s = {"first_name": "John","last_name": "Doe","address": {"street_number": "1111","street_name": "Baker Street","city": "Vienna","state": "VA","zip": "22182"}} js = json.dumps(s) #js = json.dumps({"first_name": firstname, "last_name": lastname, "address": {"street_number":streetnum,"street_name":streetname, "city": city, "state": state, "zip": zipcode}}, sort_keys=False, indent=4, separators=(',', ': ')) headers = {"Content-type": "application/json", "Accept": "application/json"} client.request("POST", "/customers?key=871a79ef8082ee4538025e07513c9c5f",body=js,headers=headers)
def mymsfHosts(myid): jobs[myid]='<started>' listout=[] msferror=False global msftoken msfToken() #return a dict of all hosts info client = http.client.HTTPConnection('localhost',55552) params=msgpack.packb(['db.hosts',msftoken,[]]) client.request("POST","/api/",params, msfheaders) response = client.getresponse() if response.status == 200: resdata=msgpack.unpackb(response.read()) if b'error_message' in resdata.keys(): msferror=True print(resdata[b'error_message']) # print(resdata) #{b'hosts': ({b'info': b'', b'os_sp': b'', b'os_lang': b'', b'os_name': b'', b'name': b'', b'created_at': 1325119167, b'updated_at': 1325119167, b'mac': b'00:16:C6:46:28:18', b'state': b'alive', b'purpose': b'', b'address': b'192.168.10.10', b'os_flavor': b''},)} else: print("db.hosts failed") msferror=True if not msferror: if len(resdata[b'hosts'])>0: for host in resdata[b'hosts']: ahost={} ahost['name']=bytes.decode(host[b'name']) ahost['address']=bytes.decode(host[b'address']) listout.append(ahost) #print(bytes.decode(host[b'name']),bytes.decode(host[b'address'])) jobs[myid]=listout
def mymsfConsoleWrite(myid,msfcommand): jobs[myid]='<started>' listout=[] msfToken() msfConsole() #command(s) to run in the msfconsole #send multiple like so: #command=r"""one #two #three""" if '\n' not in msfcommand: #add a line feed, most likely a single line command msfcommand+="\r\n" if type(msfcommand)!=bytes: msfcommand=msfcommand.encode('ascii') #print(msfcommand) client = http.client.HTTPConnection('localhost',55552) params=msgpack.packb(['console.write',msftoken,msfconsoleid,msfcommand]) client = http.client.HTTPConnection('localhost',55552) client.request("POST","/api/",params, msfheaders) response = client.getresponse() if response.status == 200: #listout.append(msgpack.unpackb(response.read())) resdata=msgpack.unpackb(response.read()) #should be like this: {b'wrote': 33} jobs[myid]=listout
def play(room_id, token, server, debug=False, alias=''): """ Connect to game server and play rounds in the loop until end of game. """ # connect to the game server client = http.client.HTTPConnection(server) client.connect() game_url = '/games/{}/board?token={}'.format(room_id, token) if alias: game_url += '&alias={}'.format(alias) # wait until the game starts client.request('GET', game_url) response = client.getresponse() while response.status == 200: data = json.loads(response.read().decode()) if debug: print(data) # sleep 3 seconds so, you will be able to read printed data time.sleep(3) # make your move and wait for a new round client.request('POST', game_url, json.dumps(get_move(data))) response = client.getresponse()
def agregarChangeset(): try: #Armando XML root = Element('osm') changeset = SubElement(root, 'changeset') changeset.set('version', '0.6') tag = SubElement(changeset, 'tag') tag.set('k', 'comment') tag.set('v', 'test') client = http.client.HTTPSConnection(osmRestApiUrl) client.request('PUT', '/api/0.6/changeset/create', tostring(root), osmHeaders()) response = client.getresponse() if response.status == 200: return str(response.read(), 'utf-8') else: raise TypeError("Error Al intentar Crear Changeset OSM: " + str(response.read(), 'utf-8')) except: raise TypeError("Error Al intentar Crear Changeset OSM " + str(response.read(), 'utf-8'))
def do_GET(self): client = http.client.HTTPConnection(urlBlocks(str(sys.argv[2]))[0], timeout=1) try: client.request( "GET", urlBlocks(str(sys.argv[2]))[1], None, self.headers ) except socket.timeout: self.sendResponse({"code": "timeout"}) else: resp = dict() clientResponse = client.getresponse() resp["headers"] = clientResponse.getheaders() resp["code"] = clientResponse.getcode() body = clientResponse.read() try: try: json.loads(body.decode()) decoded = True except ValueError: decoded = False except UnicodeDecodeError: resp["content"] = str(body) else: if decoded: resp["json"] = json.loads(body.decode()) else: resp["content"] = str(body.decode()) self.sendResponse(resp)
def request(url, port, method, path, body=None, headers=None, **kwargs): ''' Make a secure HTTPS request as JSON and return JSON This is a thin wrapper around http.client.HTTPSConnection to send/receive JSON and to bypass SSL verification and hostname verification. Works on Python 3.4 and 3.4.3. ''' headers = headers or {'Accept': 'application/json', 'Content-Type': 'application/json',} ssl_config = {'check_hostname': False} if hasattr(ssl, '_create_unverified_context'): ssl_config['context'] = ssl._create_unverified_context() if not body is None: body = json.dumps(body) client = http.client.HTTPSConnection(url, port, **ssl_config) client.request(method, path, body, headers=headers) response = client.getresponse() if not str(response.status).startswith('2'): msg = 'Unsuccessful Response. Status: {0}'.format(response.status) raise Exception(msg) return response
def save_number(self, description = '', stype = '', snumber = '', sname = '', dtype = '', dnumber = '', owner = '', callerid = '', ): """ Update feature code or number details """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','save_number') enswitch_headers = '<enswitch:description>%s</enswitch:description>' % (description) enswitch_headers += '<enswitch:stype>%s</enswitch:stype>' % (stype) enswitch_headers += '<enswitch:snumber>%s</enswitch:snumber>' % (snumber) enswitch_headers += '<enswitch:sname>%s</enswitch:sname>' % (sname) enswitch_headers += '<enswitch:dtype>%s</enswitch:dtype>' % (dtype) enswitch_headers += '<enswitch:dnumber>%s</enswitch:dnumber>' % (dnumber) enswitch_headers += '<enswitch:owner>%s</enswitch:owner>' % (owner) enswitch_headers += '<enswitch:callerid>%s</enswitch:callerid>' % (callerid) body = body.replace('<<enswitch_headers>>',enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def getHttpContent(client, url): try: client.request("GET", url) res = client.getresponse() except socket.error as e: print("Could not even connect. Upsilon may not be running at this address & port.") print(("Socket error: " + str(e))) sys.exit() except client.BadStatusLine as e: print("Connected, but could not parse HTTP response.") print("If this server is running SSL, try again with --ssl") sys.exit() if res.status == 302 and res.getheader('Location'): res.read() return getHttpContent(client, "/" + res.getheader('Location')); if res.status == 301: res.read() parts = urlparse(res.getheader('Location')) location = parts.path return getHttpContent(client, location); if res.status != 200: logging.error("Requested: %s, Expected HTTP 200, got HTTP %d" % (url, res.status)) res = res.read() assert len(res) > 0, "Expected non-empty response." return res
def filter_out_no_restaurant(): key = sys.argv[1] client = http.client.HTTPSConnection('api.yelp.com', timeout=100) header = {'Authorization': 'Bearer ' + key} ids = set() with open(os.path.join(data_dir, 'restaurants.csv'), 'r') as f: reader = csv.reader(f) next(reader) for line in reader: ids.add(line[1]) added = set() with open(os.path.join(data_dir, 'restaurant_info_with_migrated.csv'), 'r') as f1: with open(os.path.join(data_dir, 'restaurant_info_all.csv'), 'w') as f2: writer = csv.writer(f2) writer.writerow(['business_id', 'image_url', 'phone', 'price']) reader = csv.reader(f1) next(reader) for line in reader: if (line[0] in ids) and (line[0] not in added): writer.writerow(line) added.add(line[0]) for id in ids: if id not in added: client.request('GET', '/v3/businesses/' + id, headers=header) response = client.getresponse().read() response = json.loads(response.decode('utf-8')) if 'error' in response.keys(): if response['error']['code'] == 'BUSINESS_MIGRATED': new_business_id = response['error'][ 'new_business_id'] client.request('GET', '/v3/businesses/' + new_business_id, headers=header) response = client.getresponse().read() response = json.loads(response.decode('utf-8')) image_url = response[ 'image_url'] if 'image_url' in response.keys( ) else '' phone = response[ 'display_phone'] if 'display_phone' in response.keys( ) else '' price = response[ 'price'] if 'price' in response.keys() else '' writer.writerow([id, image_url, phone, price]) else: writer.writerow([id, '', '', '']) else: image_url = response[ 'image_url'] if 'image_url' in response.keys( ) else '' phone = response[ 'display_phone'] if 'display_phone' in response.keys( ) else '' price = response['price'] if 'price' in response.keys( ) else '' writer.writerow([id, image_url, phone, price])
def call(meth, option): global authenticated global token if meth != "auth.login": if not authenticated: print('MsfRPC: Not Authenticated') exit(1) # 認証以外のAPIの場合は、オプションに認証済みトークンを入れる if meth != "auth.login": option.insert(0, token) # オプションの組み立て option.insert(0, meth) # パラメータをmsgpackでシリアライズ params = msgpack.packb(option) # RPC APIの実行 client.request("POST", "/api/", params, {"Content-type": "binary/message-pack"}) # RPC APIの実行結果を返却 # 実行結果はmsgpackからデシリアライズ resp = client.getresponse() return msgpack.unpackb(resp.read())
def make_call(self, stype, snumber, callerid1, ctype, cnumber, callerid2, card, wait, warn1, warn2,screen1,screen2,answer1, answer2, recordgroup): """ Initiate a call from Telviva """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','make_call') enswitch_headers = '<enswitch:stype>%s</enswitch:stype>' % (stype) enswitch_headers += '<enswitch:snumber>%s</enswitch:snumber>' % (snumber) enswitch_headers += '<enswitch:callerid1>%s</enswitch:callerid1>' % (callerid1) enswitch_headers += '<enswitch:ctype>%s</enswitch:ctype>' % (ctype) enswitch_headers += '<enswitch:cnumber>%s</enswitch:cnumber>' % (cnumber) enswitch_headers += '<enswitch:callerid2>%s</enswitch:callerid2>' % (callerid2) enswitch_headers += '<enswitch:card>%s</enswitch:card>' % (card) enswitch_headers += '<enswitch:wait>%s</enswitch:wait>' % (wait) enswitch_headers += '<enswitch:warn1>%s</enswitch:warn1>' % (warn1) enswitch_headers += '<enswitch:warn2>%s</enswitch:warn2>' % (warn2) enswitch_headers += '<enswitch:screen1>%s</enswitch:screen1>' % (screen1) enswitch_headers += '<enswitch:screen2>%s</enswitch:screen2>' % (screen2) enswitch_headers += '<enswitch:answer1>%s</enswitch:answer1>' % (answer1) enswitch_headers += '<enswitch:answer2>%s</enswitch:answer2>' % (answer2) enswitch_headers += '<enswitch:recordgroup>%s</enswitch:recordgroup>' % (recordgroup) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def downloadfile(url, path): domain = "www.trainchinese.com" client = http.client.HTTPConnection(domain) client.request("GET", url) response = client.getresponse() with open(path, "wb") as outputfile: outputfile.write(response.read())
def get_public_trade_session(self): """ Get public trade session information """ client = self.__create_http_client() client.request('GET', '/api/v1/public/tradesession', None, self.__get_http_public_headers()) response = json.loads(self.__decode_response(client.getresponse())) return response
def clientRequest(self, method, url, postdata='\r\n', server=''): if server == '': server = self.server client = http.client.HTTPConnection(server) client.request(method, url, postdata, {"Content-Type": "application/x-www-form-urlencoded"}); resp = client.getresponse() cont = resp.read().decode('utf-8') return cont
def get_public_all_ticks(self): """ Get list of all available public feed ticks """ client = self.__create_http_client() client.request('GET', '/api/v1/public/tick', None, self.__get_http_public_headers()) response = json.loads(self.__decode_response(client.getresponse())) return response
def makeLocate(city): client = http.client.HTTPConnection(ipaddress, 5555) print("Locating Drivers nearby:") client.request("GET", "/locate?city=" + city) res = client.getresponse() data = res.read().decode('utf-8') return json.loads(data)
def do_POST(self): data = self.rfile.read(int(self.headers.get('content-length', 0))) try: jsonData = json.loads(data) except ValueError: self.sendResponse({"code": "invalid json"}) return if "url" not in jsonData.keys() or \ ("type" in jsonData.keys() and jsonData["type"] == "POST" and "content" not in jsonData.keys()): self.sendResponse({"code": "invalid json"}) return if "timeout" in jsonData.keys(): timeout = jsonData["timeout"] else: timeout = 1 if "type" in jsonData.keys() and jsonData["type"] == "POST": body = jsonData["content"] else: body = None if not isinstance(body, str) and body is not None: body = json.dumps(body) if "headers" in jsonData.keys(): headers = jsonData["headers"] else: headers = dict() client = http.client.HTTPConnection(urlBlocks(jsonData["url"])[0], timeout=timeout) try: client.request(jsonData["type"], urlBlocks(jsonData["url"])[1], body, headers) except socket.timeout: self.sendResponse({"code": "timeout"}) else: resp = dict() clientResponse = client.getresponse() resp["code"] = clientResponse.getcode() resp["headers"] = clientResponse.getheaders() body = clientResponse.read() try: try: json.loads(body.decode()) decoded = True except ValueError: decoded = False except UnicodeDecodeError: resp["content"] = str(body) else: if decoded: resp["json"] = json.loads(body.decode()) else: resp["content"] = str(body.decode()) self.sendResponse(resp)
def get_phone(self, phone): client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_phone') enswitch_headers = '<enswitch:phone>%s</enswitch:phone>' % (phone) body = body.replace('<<enswitch_headers>>',enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def delete_queue_destinations(self, queue_id): client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','delete_queue_destination') enswitch_headers = '<enswitch:queue>%s</enswitch:queue>' % (queue_id) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def _rest_method(self, endpoint, method, data=None): encoded_data = urllib.parse.urlencode(data) if data else None client = http.client.HTTPSConnection(get_hostname(self.token)) client.request(method.upper(), endpoint, encoded_data, headers=get_authorization_header(self.token)) response = client.getresponse() data = response.read().decode('utf-8') client.close() return data
def download_labyrinth(): """calls /start and download labyrinth to a file. The file name is returned""" client.request('GET', '/start') start_response = client.getresponse() with open("labyrinth", 'wb') as file: bytes = start_response.read(200) file.write(bytes) return "labyrinth"
def get_all_trades(self): """ Get list of all available trades """ client = self.__create_http_client() method = 'GET' url_relative = '/api/v1/trade' url_absolute = 'https://{0}{1}'.format(self.__web_api_address, url_relative) client.request(method, url_relative, None, self.__get_http_hmac_headers(method, url_absolute, None)) response = json.loads(self.__decode_response(client.getresponse())) return response
def get_conference(self, confid): """ Get conference based on the id """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_conference') enswitch_headers = '<enswitch:confid>%s</enswitch:confid>' % (confid) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response)
def get_queue(self, queue_id): """ Return a spefic queue """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_queue') enswitch_headers = '<enswitch:id>%s</enswitch:id>' % (queue_id) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def get_public_currency(self, currency): """ Get public currency by name Keyword arguments: currency -- currency name """ currency = urllib.parse.quote_plus(urllib.parse.quote_plus(currency)) client = self.__create_http_client() client.request('GET', '/api/v1/public/currency/{0}'.format(currency), None, self.__get_http_public_headers()) response = json.loads(self.__decode_response(client.getresponse())) return response
def get_public_tick(self, symbol): """ Get public feed tick by name Keyword arguments: symbol -- symbol name """ symbol = urllib.parse.quote_plus(urllib.parse.quote_plus(symbol)) client = self.__create_http_client() client.request('GET', '/api/v1/public/tick/{0}'.format(symbol), None, self.__get_http_public_headers()) response = json.loads(self.__decode_response(client.getresponse())) return response
def delete_number(self, snumber): """ Delete a feature code or number """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','delete_number') enswitch_headers += '<enswitch:snumber>%s</enswitch:snumber>' % (snumber) body = body.replace('<<enswitch_headers>>',enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def get_cos_outroutes(self, cos): """ Get a list of exceptions """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_cos_outroutes') enswitch_headers = '<enswitch:cos>%s</enswitch:cos>' % (cos) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def get_people(self, customer=0): """ Get a list people from a customer """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_people') enswitch_headers = '<enswitch:customer>%s</enswitch:customer>' % (customer) body = body.replace('<<enswitch_headers>>', enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def get_remote_access(self, access_id): """ Get a list of remote access accounts """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_access') enswitch_headers = '<enswitch:id>%s</enswitch:id>' % (access_id) body = body.replace('<<enswitch_headers>>',enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data
def get_numbers(self): """ Return a list of numbers and feature codes """ client = http.client.HTTPConnection(self.host) body = self.request.replace('<<method>>','get_numbers') enswitch_headers = '' body = body.replace('<<enswitch_headers>>',enswitch_headers) client.request('POST', self.url, body, headers=self.headers) response = client.getresponse() data = xmlToDictionary(response) return data