def get_code(self, session): # 使用该函数避免了手动输入code,实现了模拟用户授权后获得code的功能 conn = httplib2.HTTPSConnectionWithTimeout('api.weibo.com') postdict = { "client_id": self._appKey, "redirect_uri": self._callbackUrl, "userId": self._account, "passwd": self._password, "isLoginSina": "0", "action": "submit", "response_type": "code", } postdata = urllib.parse.urlencode(postdict) session.headers.update({ "Referer": self._author_url, 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' }) res = session.post('https://api.weibo.com/oauth2/authorize', postdata) location = res.url code = location.split('=')[1] conn.close() return code
def Translate(txt): # appid 和 secretKey 通过注册百度翻译开放平台获取:http://api.fanyi.baidu.com/api/trans/product/index appid = 'xxx' secretKey = 'xxx' httpClient = None myurl = '/api/trans/vip/translate' fromLang = 'en' toLang = 'zh' salt = random.randint(32768, 65536) sign = appid + txt + str(salt) + secretKey m1 = hashlib.md5() m1.update(sign.encode()) sign = m1.hexdigest() myurl = myurl + '?appid=' + appid + '&q=' + txt + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str( salt) + '&sign=' + sign try: httpClient = httplib2.HTTPSConnectionWithTimeout('api.fanyi.baidu.com') httpClient.request('GET', myurl) resp = httpClient.getresponse() resp = resp.read() ret = json.loads(resp) ret = ret['trans_result'][0]['dst'] except Exception as e: print(e) ret = '' finally: if httpClient: httpClient.close() return ret
def _test(): connection = httplib2.HTTPSConnectionWithTimeout("localhost", server.port) try: connection.request("GET", "/") except Exception: pass connection.close()
def html_connect(self, url): socket.setdefaulttimeout(20) try: parse = urlparse(url) if parse.scheme == "http": #self.conn=httplib.HTTPConnection(parse.netloc,timeout=60) self.conn = httplib2.HTTPConnectionWithTimeout(parse.netloc) else: #self.conn=httplib.HTTPSConnection(parse.netloc,timeout=60) self.conn = httplib2.HTTPSConnectionWithTimeout(parse.netloc) if parse.path == "": # Si no disponemos de path le ponemos la barra path = "/" elif parse.query: # Si disponemos de path y query, realizamos el montaje path = "%s?%s" % (parse.path, parse.query) else: # Si solo disponemos de path path = parse.path self.conn.request("GET", path) self.response1 = self.conn.getresponse() self.status = self.response1.status self.reason = self.response1.reason self.headers = self.response1.getheaders() except socket.error: #errno, errstr = sys.exc_info()[:2] #if errno == socket.timeout: #print "There was a timeout" #else: #print "There was some other socket error" self.status = 408 except: self.status = 404
def getData(self, path): result = None path = '/csv_data/v1' + path print(path) path = self.encodepath(path) for i in range(self.reconnectTimes): try: #set http header here self.httpClient.request('GET', path, headers={ "Authorization": "Bearer " + self.token, "Accept-Encoding": "gzip, deflate" }) #make request response = self.httpClient.getresponse() result = response.read() compressedstream = BytesIO(result) gziper = gzip.GzipFile(fileobj=compressedstream) try: result = gziper.read() except: pass if (path.find('.csv?') == -1): result = result.decode('utf-8').encode('GB18030') return response.status, result except Exception as e: if i == self.reconnectTimes - 1: raise e if self.httpClient is not None: self.httpClient.close() self.httpClient = httplib2.HTTPSConnectionWithTimeout( self.domain, self.port, timeout=60) return -1, result
def leerPagina(url): import httplib2 from urllib.parse import urlparse import os, sys parse = urlparse(url) if parse.scheme == "http": conn = httplib2.HTTPConnectionWithTimeout(parse.netloc, timeout=60) else: conn = httplib2.HTTPSConnectionWithTimeout(parse.netloc, timeout=60) if parse.path == "": # Si no disponemos de path le ponemos la barra path = "/" elif parse.query: # Si disponemos de path y query, realizamos el montaje path = "%s?%s" % (parse.path, parse.query) else: # Si solo disponemos de path path = parse.path # self.conn.putheader("User-agent", 'pywc') try: conn.request("GET", path) response = conn.getresponse() print("status: %s" % response.status) print("------------------------------------------") print("reason: %s" % response.reason) print("------------------------------------------") print("headers: %s" % response.getheaders()) print("------------------------------------------") print("html: %s" % response.read()) except: print(sys.exc_info()[1])
def stockquery(symbol): connection = httplib2.HTTPSConnectionWithTimeout('sandbox.tradier.com', 443, timeout=30) # headers = {"Accept":"application/json", # "Authorization":"Bearer " + getKey()} # TODO headers = { "Accept": "application/json", "Authorization": "Bearer " + api_key } connection.request('GET', '/v1/markets/quotes?symbols=' + symbol, None, headers) try: response = connection.getresponse() respContent = response.read() # Success print('Response status ' + str(response.status)) respContent = respContent.decode('UTF-8') # to json respContent = json.loads(respContent) # let's return the response's data as dict stockData = { 'company': respContent['quotes']['quote']['description'], 'price': respContent['quotes']['quote']['last'], 'symbol': respContent['quotes']['quote']['symbol'] } return stockData except: # Exception print('Exception during request')
def load_cities(): global CITY_CACHE if CITY_CACHE is not None: return CITY_CACHE cities = {} cache_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), CITY_CACHE_FILE) need_reload = True if os.path.exists(cache_file): with open(cache_file) as fp: cities = json.load(fp) if cities: need_reload = False if need_reload is True: conn = httplib2.HTTPSConnectionWithTimeout( URL_12306, timeout=20, disable_ssl_certificate_validation=True) conn.request("GET", url=CITY_LIST_URL_FILE) response = conn.getresponse() if response.code == 200: data = response.read().decode() for res in re.finditer( r'@[a-z]{3}\|(.+?)\|(.+?)\|[a-z]+?\|[a-z]+?\|[0-9]+?', data): city = res.group(1) city_code = res.group(2) cities[city] = city_code with open(cache_file, 'w') as cf: json.dump(cities, cf) CITY_CACHE = cities return cities
def notify(self, notification): conn = httplib2.HTTPSConnectionWithTimeout("api.prowlapp.com", 443, timeout=10) try: description = notification.incident.description details = notification.incident.details except : description = notification.message details = "" conn.request("POST", "/publicapi/add", urlencode({ "apikey": notification.user_to_notify.profile.prowl_api_key, "application" : notification.user_to_notify.profile.prowl_application or self.__config.get( 'application', 'config' ), "url" : notification.user_to_notify.profile.prowl_url or "", "priority" : self.__config.get('priority', 0), "event" : description, "description": details, }), { "Content-type": "application/x-www-form-urlencoded" }) status = conn.getresponse().status if 200 <= status < 300: print("Done") else: # todo: error handling print("Unable to connect.")
def build(host, **kwargs): proxy_info = http_proxy.GetHttpProxyInfo() if callable(proxy_info): proxy_info = proxy_info('https') return httplib2.HTTPSConnectionWithTimeout(host, proxy_info=proxy_info, **kwargs)
def _test(): connection = httplib2.HTTPSConnectionWithTimeout( 'localhost', server.port) try: connection.request('GET', '/') except Exception: pass connection.close()
def build(host, **kwargs): proxy_info = http_proxy.GetHttpProxyInfo() if callable(proxy_info): proxy_info = proxy_info('https') ca_certs = properties.VALUES.core.custom_ca_certs_file.Get() return httplib2.HTTPSConnectionWithTimeout(host.encode('idna'), proxy_info=proxy_info, ca_certs=ca_certs, **kwargs)
def _make_request(self, method, bucket='', key='', query_args={}, headers={}, data='', metadata={}): key = key.replace('\\', '') server = '' if bucket == '': server = self.server elif self.calling_format == CallingFormat.SUBDOMAIN: server = "%s.%s" % (bucket, self.server) elif self.calling_format == CallingFormat.VANITY: server = bucket else: server = self.server path = '' if (bucket != '') and (self.calling_format == CallingFormat.PATH): path += "/%s" % bucket # add the slash after the bucket regardless # the key will be appended if it is non-empty path += "/%s" % urllib.quote_plus(key, '/') # build the path_argument string # add the ? in all cases since # signature and credentials follow path args if len(query_args): path += "?" + query_args_hash_to_string(query_args) is_secure = self.is_secure host = "%s:%d" % (server, self.port) while True: if (is_secure): connection = httplib.HTTPSConnectionWithTimeout(host) else: connection = httplib.HTTPConnectionWithTimeout(host) final_headers = merge_meta(headers, metadata); # add auth header self._add_aws_auth_header(final_headers, method, bucket, key, query_args) connection.request(method, path, data, final_headers) resp = connection.getresponse() if resp.status < 300 or resp.status >= 400: return resp # handle redirect location = resp.getheader('location') if not location: return resp # (close connection) resp.read() scheme, host, path, params, query, fragment \ = urlparse.urlparse(location) if scheme == "http": is_secure = True elif scheme == "https": is_secure = False else: raise invalidURL("Not http/https: " + location) if query: path += "?" + query
def page_information(self, host, path="/"): try: conn = httplib2.HTTPSConnectionWithTimeout(host) conn.request("HEAD", path) if re.match("^[23]\d\d$", str(conn.getresponse().status)): return f' Website pages are available ' except: return None
def get_local_ip(): url = "jsonip.com" conn = httplib2.HTTPSConnectionWithTimeout(url, timeout=20) conn.request("GET", url='') response = conn.getresponse() if response.code == 200: data = response.read().decode() res = re.search('\d+\.\d+\.\d+\.\d+', data) if res: return res.group(0) return None
def run(self, msg, user): c = httplib2.HTTPSConnectionWithTimeout("api.mrbesen.de") c.request("GET", "/online.txt") response = c.getresponse() if response.code == 200: data = response.read().decode().strip() if data == '1': return 'Ich bin gerade am Computer und mache ... Sachen.' else: print( "Online api: offline - no idea what he is doing api result:", data) return None
def notify(self, notification): conn = httplib2.HTTPSConnectionWithTimeout("api.pushover.net:443") conn.request( "POST", "/1/messages.json", urlencode({ "token": notification.user_to_notify.profile.pushover_app_key, "user": notification.user_to_notify.profile.pushover_user_key, "message": notification.message, }), {"Content-type": "application/x-www-form-urlencoded"}) status = conn.getresponse().status if status >= 200 and status < 300: print("Done") else: # todo: error handling print("Unable to connect.")
def run(self, msg, user): c = httplib2.HTTPSConnectionWithTimeout("api.mrbesen.de") c.request("GET", "/pos.php") response = c.getresponse() if response.code == 200: data = response.read().decode().strip() time, loc, lastknowntime, lastknownloc = data.split("\n") if loc == 'unknown': print("unknown location") datestr = self.getTimeStr(time) return "Also am " + datestr + " war ich in " + lastknownloc + " aber grade bin ich Unterwegs." else: datestr = self.getTimeStr(time) return "Bin gerade (" + datestr + ") in " + loc return None
def getAuthToken(): credsFile = open('C:\\Users\\HassSarw\\brightcove\\brightcove_oauth.txt') creds = json.load(credsFile) conn = httplib2.HTTPSConnectionWithTimeout("oauth.brightcove.com") url = "/v4/access_token" params = {"grant_type": "client_credentials"} client = creds["client_id"] client_secret = creds["client_secret"] b = bytes(('%s:%s' % (client, client_secret)).replace('\n', ''), 'utf-8') authString = base64.b64encode(b) requestUrl = url + "?" + urllib.parse.urlencode(params) headersMap = { "Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic " + authString.decode('utf-8') } conn.request("POST", requestUrl, headers=headersMap) response = conn.getresponse() return json.loads(response.read())['access_token']
def ticket_search(to_city, from_city=None, do_date=None, student=False): ticket_type = "ADULT" if from_city == None: from_city = get_local_location() print("Using local city:", from_city) if do_date == None: do_date = get_default_date() print("Using current date:", do_date) else: print(do_date) do_date = format_input_date(do_date) print(do_date) if not check_city(from_city): print("暂不支持该城市:", from_city) exit(-1) if student: ticket_type = '0X00' print(do_date, ":", from_city, "->", to_city, "正在查询...") from_city = load_cities()[from_city] to_city = load_cities()[to_city] conn = httplib2.HTTPSConnectionWithTimeout( URL_12306, timeout=30, disable_ssl_certificate_validation=True) url_path = '/otn/leftTicket/query?\ leftTicketDTO.train_date={train_time}&\ leftTicketDTO.from_station={from_city}&\ leftTicketDTO.to_station={to_city}&\ purpose_codes={ticket_type}' url_path = url_path.format(train_time=do_date, from_city=from_city, to_city=to_city, ticket_type=ticket_type) # print(url_path) conn.request("GET", url_path) response = conn.getresponse() if response.code == 200: data = json.loads(response.read()) # print(data["status"]) if not "data" in data: return "未查询到相关车次" return data["data"]
def __init__(self, host, port, cert_file, key_file, session=None, header_get=None, header_post=None): self.h = httplib2.HTTPSConnectionWithTimeout( host, port, key_file=key_file, cert_file=cert_file, disable_ssl_certificate_validation=True ) self.header_get = header_get if header_get else { 'Connection': 'close', } self.header_post = header_post if header_post else { 'Content-Type': 'application/json', 'Connection': 'close', } if session == "uuid": session = uuid.uuid4().hex if session: self.header_get['Cookie'] = 'session = %s' % session self.header_post['Cookie'] = 'session = %s' % session
def addUtterances(): configEntites = [] result = True for file in os.listdir(dir): if result and file.endswith(".txt"): utterances = [] intent = os.path.splitext(file)[0] print("Adding utterances for " + intent) with open(file, "r") as intentFile: for example in intentFile: entityLabels = [] utterances.append({ "text": example.replace("(", "").replace(")", ""), "intentName": intent, "entityLabels": entityLabels }) if len(utterances) > 0: try: print(len(utterances)) conn = httplib2.HTTPSConnectionWithTimeout( "westus.api.cognitive.microsoft.com") conn.request( "POST", "/luis/api/v2.0/apps/{0}/versions/0.1/examples".format( appid), json.dumps(utterances), headers) response = conn.getresponse() print(response.status) conn.close() print(response.status) result = response.status == 201 except Exception as e: print(e) result = False return result
def parseMeeting(data, i): connection = httplib2.HTTPSConnectionWithTimeout('api.parse.com', 443) connection.connect() connection.request('POST', '/1/classes/Meeting', json.dumps({ "Name": data["eventsCollection"][i]['name']['text'], "Description": data["eventsCollection"][i]['description']['text'], "StartDate": {"__type": "Date", "iso": data["eventsCollection"][i]['start']['local']}, "EndDate": {"__type": "Date", "iso": data["eventsCollection"][i]['end']['local']}, "Address": data["eventsCollection"][i]['venue']['longaddress'], "Location": { "__type": "GeoPoint", "latitude": float(data["eventsCollection"][i]['venue_latlong'][0]), "longitude": float(data["eventsCollection"][i]['venue_latlong'][1]) }, "MeetingPicUrl": data["eventsCollection"][i]['logo']['url'], "StartUtc": {"__type": "Date", "iso": data["eventsCollection"][i]['start']['utc']}, "EndUtc": {"__type": "Date", "iso": data["eventsCollection"][i]['end']['utc']}, "Timezone": data["eventsCollection"][i]['start']['timezone'], "Url": data["eventsCollection"][i]['url'], "IsVisible": data["eventsCollection"][i]['shareable'], "Price": data["eventsCollection"][i]['price_range'], "Currency": data["eventsCollection"][i]['currency'], "Category": data["eventsCollection"][i]['category']['name'] if 'category' in data["eventsCollection"][i] else "", # "EventType": {"__type": "Pointer", "className": "EventType", "objectId": "11PJfsLKVG"}, "EventType": data["eventsCollection"][i]['format']['short_name'], "Capacity": data["eventsCollection"][i]['capacity'], "NextOccurrence": {"__type": "Date", "iso": data["eventsCollection"][i]['next_occurrence']['local']}, "CreatedBy": { "__type": "Pointer", "className": "_User", "objectId": "u7mg8bh3Dl" }, "MeetingType": "OPEN", "Type": "EVENT" }), { "X-Parse-Application-Id": parse_keys.PARSE_APP_ID, "X-Parse-REST-API-Key": parse_keys.REST_KEY, "Content-Type": "application/json" }) res = str(connection.getresponse().read()) print(res)
def addUtterances(): configEntites = [] result = True for file in os.listdir(dir): if result and file.endswith(".txt"): utterances = [] intent = os.path.splitext(file)[0] print("Adding utterances for " + intent) with open(file, "r") as intentFile: for example in intentFile: entityLabels = [] # Check if example has entities exampleSplit = example.strip().split("<=>") exampleText = exampleSplit[0].strip() #print(exampleSplit) if len(exampleSplit) == 1: exampleEntities = exampleSplit[0:] #print(exampleEntities) # check if entities mentioned in text exist in config """for exampleEntity in exampleEntities: if not exampleEntity.strip() in configEntites: print("The entity " + exampleEntity + " used in " + exampleText + " is not present in config") return None""" # Check if parantheses match openParanCount = exampleText.count("(") closeParanCount = exampleText.count(")") """if openParanCount != closeParanCount: print("Paranthesis don't match for " + exampleText) return None""" # Check if paranthesis and provide entities match """if openParanCount != len(exampleEntities): print("The entities provided and the words marked in paranthesis don't match for " + exampleText) return None startPos = 0 entitiesCount = 0 noOfEntities = len(exampleEntities)""" """while entitiesCount < noOfEntities: startPos = exampleText.find("(", startPos, len(exampleText)) + 1 endPos = exampleText.find(")", startPos, len(exampleText)) - 1 entityLabel = {"EntityType": exampleEntities[entitiesCount].strip(), "StartToken": startPos - ((entitiesCount * 2) + 1), "EndToken": endPos - ((entitiesCount * 2) + 1)} entitiesCount += 1 entityLabels.append(entityLabel)""" utterances.append({ "text": exampleText.replace("(", "").replace(")", ""), "intentName": intent, "entityLabels": entityLabels }) if len(utterances) > 0: try: print(utterances) conn = httplib2.HTTPSConnectionWithTimeout( "westus.api.cognitive.microsoft.com") conn.request( "POST", "/luis/api/v2.0/apps/{0}/versions/0.1/examples".format( appid), json.dumps(utterances), headers) response = conn.getresponse() conn.close() print(response.status) result = response.status == 201 except Exception as e: print(e) result = False return result
def test_timeout_https(): c = httplib2.HTTPSConnectionWithTimeout("localhost", 80, timeout=47) assert 47 == c.timeout
def get_webpage(site, page): conn = httplib2.HTTPSConnectionWithTimeout(site) conn.request("GET", page) rd = conn.getresponse() print(rd.status, rd.reason) return rd.read()
elif danger and re.match(danger, args.subject): color = "danger" # send API request options = { "attachments": [ { "fallback": args.subject + "\n" + args.message, "color": color, "title": args.subject, "text": args.message, } ], "username": os.environ["SLACK_USERNAME"], "icon_emoji": os.environ["ICON_EMOJI"], } conn = httplib2.HTTPSConnectionWithTimeout("hooks.slack.com:443") conn.request( "POST", f"/services/{os.environ['SLACK_WEBHOOK_URL']}", json.dumps(options), {"Content-type": "application/json"}, ) res = conn.getresponse() if res.status != 200: print("Slack API returned error: " + res.read(), end="\n", file=stderr) exit(1)
# automating connection using http import httplib2 c = httplib2.HTTPSConnectionWithTimeout('docs.python.org', 443) c.putrequest("GET", "/3/tutorial/index.html") c.putheader('Someheader', 'Somevalue') c.endheaders() r = c.getresponse() data = r.read() print(data) c.close()
def checkURL(url): p = urlparse(url) conn = httplib2.HTTPSConnectionWithTimeout(p.netloc) conn.request('HEAD', p.path) resp = conn.getresponse() return resp.status != 404
import httplib2 import json intent=input("enter new intent to be created") headers = {"Ocp-Apim-Subscription-Key": '3f88533f3ac04af787adaeb946f26956', "Content-Type": "application/json"} appid='73dc6ed9-6092-4203-99bb-61789ef88fea' conn = httplib2.HTTPSConnectionWithTimeout("api.projectoxford.ai") conn.request("POST", "/luis/v2.0/apps/{0}/versions/0.1/intents".format(appid), json.dumps({"Name": intent}), headers) response = conn.getresponse() print(response.status)