class GoogleDictionary(): def __init__(self): self.host = 'translate.google.com' self.connection = HTTPSConnection(self.host) self.empty_comma = re.compile(r',(?=,)') def lookup(self, lookedup_word, src_language='ru', dst_language='en'): secret_token = get_current_token(lookedup_word) url = self.format_lookup_url(lookedup_word, secret_token, src_language, dst_language) self.connection.request("GET", url) response = self.connection.getresponse() response_content = response.read().decode('utf8') #json_obj = json.loads(self.empty_comma.subn('', response_content)[0].replace(u'\xA0', u' ').replace('[,', '[1,')) response_content = response_content.replace(',,', ',"",'); response_content = response_content.replace(',,', ',"",'); response_content = response_content.replace('[,', '["",'); response_content = response_content.replace(',]', ',""]'); response_content = response_content.replace('\xA0', ' ') #fixed_content = self.empty_comma.subn('', response_content)[0].replace('\xA0', ' ') return json.loads(response_content) def format_lookup_url(self, word, secret_token, src_language, dst_language): url = '/translate_a/single?client=t&sl={0}&tl={1}&hl=en&dt=at&dt=bd&dt=ex&' \ 'dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=2&' \ 'rom=1&ssel=3&tsel=3&kc=1&tk={2}&q={3}'.format(src_language, dst_language, secret_token, quote(word)) return url def unpack(self, json_obj): window_content = json_obj[0] if len(json_obj) > 1: article_by_pos = json_obj[1] defs = {} for article in article_by_pos: pos = article[0] definition = article[1] defs[pos] = definition else: defs = { 'unk', window_content[0][0] } return defs def get_pronunciation_url(self, word, language): secret_token = get_current_token(word) url_sound ="https://translate.google.com/translate_tts?ie=UTF-8&client=t&tk={0}&tl={1}&q={2}". \ format(secret_token, language, quote(word)) return url_sound def get_sound_file(self, word, language): url_sound = self.get_pronunciation_url(word, language) try: self.connection.request("GET", url_sound) except ImproperConnectionState: # reconnect and try again self.connection.close() self.connection.connect() self.connection.request("GET", url_sound) response = self.connection.getresponse() content_type = response.headers['Content-Type'] return response.read(), content_type
def send(self, message): """ Sends a specified message with id "message" or as object. """ # Only send message if message is instance of PushoverMessage if type(message) is PushoverMessage: # get dict contaning "message" and any other key-values declared # by the "set" method. data = message.get() data['token'] = self.token # define which user to send notification to if any if self.user is not None: data['user'] = self.user_token # define which device to send notification to if any if self.user_device is not None: data['device'] = self.user_device # initialise HTTPS connection conn = HTTPSConnection(self.PUSHOVER_SERVER) # send HTTPS request conn.request( "POST", self.PUSHOVER_ENDPOINT, urlencode(data), self.PUSHOVER_CONTENT_TYPE ) # get response form server response = conn.getresponse().read().decode('utf-8') data = json.loads(response) # check response for error if data['status'] != 1: raise PushoverError(response) else: return True else: raise PushoverError("Wrong type passed to Pushover.send()!")
def submit_xml_request(xml_file): ''' Submit the xml request to MAST. Parameters: xml_file : string The xml request string. Returns: response : httplib object The xml request submission result. ''' home = os.environ.get("HOME") user = os.environ.get("USER") signer = SignStsciRequest() request_xml_str = signer.signRequest("{0}/.ssh/privkey.pem".format(home), xml_file) params = urllib.urlencode({ "dadshost": "dmsops1.stsci.edu", "dadsport": 4703, "mission": "HST", "request": request_xml_str}) headers = {"Accept": "text/html", "User-Agent": "{0}PythonScript".format(user)} req = HTTPSConnection("archive.stsci.edu") req.request("POST", "/cgi-bin/dads.cgi", params, headers) response = req.getresponse().read() return response
def add(request): instance = Person(verified=False, ip="127.0.0.1", last_updated=datetime.datetime.utcfromtimestamp(0)) form = AddPersonForm(request.POST or None, instance=instance) if form.is_valid(): person = form.save(commit=False) ssl_context = ssl.create_default_context() ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_NONE host = resolve_location(person) connection = HTTPSConnection(host=host, port=8080, context=ssl_context) connection.request("GET", reverse("directory:info")) response = connection.getresponse() data = JSONParser().parse(response) cert = connection.sock.getpeercert() connection.close() serializer = PublicPersonSerializer(person, data=data) cn = None #for key, value in cert["subject"]: # if key == "commonName": # cn = value # break #if cn == person.location and serializer.is_valid(): if serializer.is_valid(): serializer.save() return HttpResponseRedirect(reverse("directory:index")) context = {"form": form, "section": "directory"} return render(request, "person_form.html", context)
def _is_online(domain, sub_path, response_status, response_reason): conn = HTTPSConnection(domain, timeout=1) conn.request("HEAD", sub_path) response = conn.getresponse() conn.close() return (response.status == response_status) and (response.reason == response_reason)
def call(self, method='', params=None): """ Call T411 API """ if params is not None: params = urlencode(params) headers = HTTP_HEADERS if method != 'auth': headers['Authorization'] = self._token conn = HTTPSConnection(API_URL) conn.request('POST', '/%s' % method, body=params, headers=headers) r = conn.getresponse() if r.status == HTTP_OK: #import pdb; pdb.set_trace() rt = r.read() try: pass rt = rt.decode('utf-8') conn.close() except: conn.close() return rt else: conn.close() return False
def _send_post(self, url, payload={}): payload = copy.copy(payload) # avoid modifying the original dict # add some stuff to the payload payload['nonce'] = int(time.time()*1e6) body = urllib.parse.urlencode(payload).encode() # convert to bytes object sig = hmac.new(base64.b64decode(self._secret), body, hashlib.sha512).digest() sig_b64 = base64.b64encode(sig) headers = { 'bitfloor-key': self._key, 'bitfloor-sign': sig_b64, 'bitfloor-passphrase': self._passphrase, 'bitfloor-version': self._api_version, 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': len(body) } conn = HTTPConn(self._host, self._order_port) conn.request("POST", url, body, headers) resp = conn.getresponse() s = resp.read().decode() # comes as a bytes object, decode to a string conn.close() return json.loads(s)
def _send(self, message): """ Sends the specified PushoverMessage object via the Pushover API. """ kwargs = message.get() kwargs['token'] = self.token assert 'message' in kwargs assert self.token is not None if not 'user' in kwargs: if self.set_user is not None: kwargs['user'] = self.user_token if self.user_device is not None: kwargs['device'] = self.user_device else: kwargs['user'] = os.environ['PUSHOVER_USER'] data = urlencode(kwargs) conn = HTTPSConnection(Pushover.PUSHOVER_SERVER) conn.request("POST", Pushover.PUSHOVER_ENDPOINT, data, Pushover.PUSHOVER_CONTENT_TYPE) output = conn.getresponse().read().decode('utf-8') data = json.loads(output) if data['status'] != 1: raise PushoverError(output) else: return True
def send(self, type, text): message_time = time.time() message_timestamp = time.ctime(message_time) if check_time_restriction(self.starttime, self.endtime): self.msg_to_send = text[:10000].encode('utf8') + " Message Sent at: " + message_timestamp self.event = NMA_EVENT.encode('utf8') self.content_type = NMA_CONTENT_TYPE notify_data = { 'application': self.app_name, 'description': self.msg_to_send, 'event': self.event, 'priority': self.priority, 'content-type': self.content_type, 'apikey': self.api_key } headers = { 'User-Agent': NMA_USER_AGENT } headers['Content-type'] = NMA_HEADER_CONTENT_TYPE http_handler = HTTPSConnection(NMA_URL) http_handler.request(NMA_METHOD, NMA_PATH, urlencode(notify_data), headers) http_response = http_handler.getresponse() try: res = self._parse_response(http_response.read()) except Exception as e: res = { 'type': 'NMA Notify Error', 'code': 800, 'message': str(e) } current_app.logger.info('Event NotifyMyAndroid Notification Failed: {0}'.format(str(e))) raise Exception('NotifyMyAndroid Failed: {0}' . format(str(e)))
def post_mp_request(self,data,files,extra_path=""): # Create the multipart request body buffer_fname = tempfile.mktemp() buffer_fp = open(buffer_fname,'wb') mpw = MultiPartWriter(buffer_fp) mpw.add_attachment(data.encode('utf-8'),'application/json, charset=UTF-8','body') for cid,fp in files.items(): mpw.add_attachment(fp,'application/octet-stream',cid) mpw.done() buffer_fp.close() headers = { "Content-type": b'multipart/related; boundary=' + mpw.boundary, "Accept": b'application/json,multipart/related'} if self.scheme.lower()=='https': conn = HTTPSConnection(self.hostname,self.port) else: conn = HTTPConnection(self.hostname,self.port) req_path = self.path + '/' + extra_path buffer_fp = open(buffer_fname,'rb') conn.request("POST", req_path, buffer_fp, headers) buffer_fp.close() jsonwsp_response = self.parse_response(conn.getresponse()) conn.close() os.unlink(buffer_fname) return jsonwsp_response
def ping_url(protocol, url): url = to_string(url) protocol = to_string(protocol).lower() url_parts = url.split('/', 1) host = url_parts[0] if len(url_parts) == 1: path = '/' else: path = '/%s' % url_parts[1] if protocol == 'https': connection = HTTPSConnection(host) elif protocol == 'http': connection = HTTPConnection(host) else: raise ValueError('url', 'Invalid protocol %s. Use only http or https.' % protocol) valid_url = False try: connection.request('HEAD', path) response = connection.getresponse() except Exception: pass else: if response.status != 404: valid_url = True finally: connection.close() return valid_url
def query(self, endpoint, arguments=None): if not endpoint.startswith('/'): endpoint = '/' + endpoint if not endpoint.endswith(ApiClient.ENDPOINT_EXT): endpoint += ApiClient.ENDPOINT_EXT if arguments is None: arguments = {} query_string = '?'.join([endpoint, urlencode(arguments)]) result = self.cache.get(query_string) if result is not None: return objectify.fromstring(result) connection = HTTPSConnection(ApiClient.API_HOST) connection.request('GET', query_string) response = connection.getresponse() if response.status != 200: raise ApiError('Error response: ' + str(response.status)) result = response.read() result_object = objectify.fromstring(result) cached_until = datetime.strptime(result_object.cachedUntil.text, ApiClient.TIMESTAMP_FORMAT) self.cache.set(query_string, result, cached_until) return result_object
def do_request(self, method, url, params = {}): final_url = url if method == 'GET' and params: final_url += '?' + urlencode(params) request_body = None elif (method == 'POST' or method == 'PUT') and params: request_body = urlencode(params) else: request_body = None print("Starting {method} {final_url}".format(method = method, final_url = final_url)) if self.use_ssl: con = HTTPSConnection(self.base_url) else: con = HTTPConnection(self.base_url) headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["ISV_API_KEY"] = self.key headers["ISV_API_SECRET"] = self.secret headers["LCNS_DISABLE_SIGN"] = "true" con.request(method, final_url, request_body, headers) res = con.getresponse() body = res.read() status = res.status if self.debug: debug_message = "" debug_message += "################################\n" debug_message += "REQUEST: {method} {uri}\n".format(method = method.upper(), uri = self.request_url(final_url) ) if params and ( method == 'POST' or method == 'GET' ): debug_message += "PARAMETERS: {params}\n".format(params = params) debug_message += "RESPONSE CODE: {status}\n".format(status = status) debug_message += "BODY:\n{body}\n".format(body = body) debug_message += "================================\n" print(debug_message) return {'body': body, 'status': status}
def getRemoteDBModifiedTS(): """ Performs a HEAD request to get the Last-Modified date-time of a database dump file and parses it into a UNIX timestamp. """ debug_msg = "Unable to get timestamp of remote database dump - {0}" logging.info("Getting timestamp of database dump at '%s'", HOST + PATH) try: # Removing the scheme from the URL conn = HTTPSConnection(HOST[8:], timeout=TIMEOUT) conn.request("HEAD", PATH) except gaierror as e: logging.debug(debug_msg.format("Cannot connect to '%s', error: %s"), HOST + PATH, e) exit(1) rsp = conn.getresponse() if rsp.status != 200: logging.debug(debug_msg.format("Server responded with: %d %s"), rsp.status, rsp.reason) exit(1) last_modified = rsp.getheader("last-modified", None) if last_modified is None: logging.debug(debug_msg.format("Response doesnt include Last-Modified Header")) exit(1) last_m_dt = datetime.strptime(last_modified.split(", ")[1], "%d %b %Y %H:%M:%S %Z") return timegm(last_m_dt.timetuple())
def submit_xml_request(xml_request): """Submit the XML request to the MAST archive. Parameters ---------- xml_request : string The request XML string. Returns ------- submission_results : httplib object The XML request submission results. """ user = os.environ.get("USER") home = os.environ.get("HOME") signer = SignStsciRequest() request_xml_str = signer.signRequest('{0}/.ssh/privkey.pem'.format(home), xml_request) params = urlencode({ 'dadshost': get_settings()['dads_host'], 'dadsport': 4703, 'mission':'HST', 'request': request_xml_str}) headers = {"Accept": "text/html", "User-Agent":"{0}PythonScript".format(user)} req = HTTPSConnection(get_settings()['archive']) req.request("POST", "/cgi-bin/dads.cgi", params, headers) response = req.getresponse().read() req.close() return response
def send(self, type, text, raw): message_time = time.time() message_timestamp = time.ctime(message_time) if check_time_restriction(self.starttime, self.endtime): if self.suppress_timestamp == False: self.msg_to_send = text[:10000].encode('utf8') + " Message Sent at: " + message_timestamp else: self.msg_to_send = text[:10000].encode('utf8') notify_data = { 'apikey': self.api_key, 'application': self.app_name, 'event': self.event, 'description': self.msg_to_send, 'priority': self.priority } if sys.version_info >= (2,7,9): http_handler = HTTPSConnection(PROWL_URL, context=ssl._create_unverified_context()) else: http_handler = HTTPSConnection(PROWL_URL) http_handler.request(PROWL_METHOD, PROWL_PATH, headers=self.headers,body=urlencode(notify_data)) http_response = http_handler.getresponse() if http_response.status == 200: return True else: current_app.logger.info('Event Prowl Notification Failed: {0}'. format(http_response.reason)) raise Exception('Prowl Notification Failed: {0}' . format(http_response.reason))
def push(event, msg): global config data = {'apikey': config["API_KEY"], 'application': __application__, 'event': event, 'description': msg, 'priority': 0} h = Https(config["API_DOMAIN"]) h.request("POST", "/publicapi/add", headers={'Content-type': "application/x-www-form-urlencoded", 'User-Agent': __application__ + '/' + str(__version__) }, body=urlencode(data)) response = h.getresponse() request_status = response.status if request_status == 200: return True else: return False
def ensure_no_open_tickets(version): version = "v%s" % version conn = HTTPSConnection("api.github.com") try: log("Checking for open tickets on Github for version %s" % version) log("Check if node is available") conn.request( "GET", "/repos/elasticsearch/elasticsearch/issues?state=open&labels=%s" % version, headers={"User-Agent": "Elasticsearch version checker"}, ) res = conn.getresponse() if res.status == 200: issues = json.loads(res.read().decode("utf-8")) if issues: urls = [] for issue in issues: urls.append(issue["url"]) raise RuntimeError("Found open issues for release version %s see - %s" % (version, urls)) else: log("No open issues found for version %s" % version) else: raise RuntimeError("Failed to fetch issue list from Github for release version %s" % version) except socket.error as e: log("Failed to fetch issue list from Github for release version %s' % version - Exception: [%s]" % (version, e)) # that is ok it might not be there yet finally: conn.close()
def get_yandex_translate(self, searching_text, to_language): try: quoted_text = quote(u"" + searching_text + "") req = '/api/v1.5/tr.json/translate?' \ 'key=trnsl.1.1.20150629T151613Z.82119cbb3415f490.ba44aa922a8b28e549144e0e2fa6249c7508358e' \ '&text=' + quoted_text \ + "&lang=" + to_language \ + '&options=1' conn = HTTPSConnection('translate.yandex.net') conn.request("GET", req) rez = conn.getresponse() data = rez.read() conn.close() if rez.status == 200: ddata = data.decode("utf-8") j = simplejson.loads(ddata, ) tr_text = ''.join(j['text']) print(tr_text) return tr_text else: print("Something is wrong: " + str(rez.status) + " status code") print(data) except HTTPException as err: print(err)
def _url_retrieve(self, url, outfd, reporthook, binary): #Like the one in urllib. Unlike urllib.retrieve url_retrieve #can be interrupted. KeyboardInterrupt exception is raised when #interrupted. count = 0 blockSize = 1024 * 8 parsed_url = urlparse(url) host = parsed_url.netloc try: connection = HTTPSConnection(host, timeout=15) headers = { "Accept-Encoding": "identity", "Host": host, "User-Agent": "Python/3" } connection.request("GET", parsed_url.path, headers=headers) urlobj = connection.getresponse() assert urlobj.getcode() == 200 totalSize = int(urlobj.info()['content-length']) while not self._is_aborted(): data = urlobj.read(blockSize) count += 1 if not data: break if not binary: data = data.decode("utf-8") outfd.write(data) ui_thread_do(reporthook, count, blockSize, totalSize) except Exception as e: raise e
def callapi(self, method, path, args): # Set headers. headers = { 'User-Agent': USER_AGENT } # Set content type if we want to post the data. if method == "POST": headers['Content-type'] = "application/x-www-form-urlencoded" # Create the HTTPSConnection and do the api call. http_handler = HTTPSConnection(API_SERVER) http_handler.request(method, path, urlencode(args), headers) # Get the response. resp = http_handler.getresponse() try: # Return the API-Response. return self._parse_reponse_json(resp.read()) except Exception as e: # Return response could not be parsed. return { 'type': "RapidPush parse error", 'code': 600, 'message': str(e) }
def httpsGet(self, url): Log.d("GET " + url) url = self.completeUrl(url) headers = self.createHeaders() conn = HTTPSConnection(self.server) conn.request("GET", url, None, headers) self.lastResponse = self.parseResponse(conn.getresponse()) return self.lastResponse
def normalise_package(name): """ """ http = HTTPSConnection('pypi.python.org') http.request('HEAD', '/pypi/%s/' % name) r = http.getresponse() if r.status not in (200, 301): raise ValueError(r.reason) return r.getheader('location', name).split('/')[-1]
def _send_get(self, url, payload={}): body = urllib.parse.urlencode(payload) conn = HTTPConn(self._host, self._data_port) conn.request("GET", url, body) resp = conn.getresponse() s = resp.read().decode() # comes as a bytes-type object, decode to string conn.close() print(s) return json.loads(s)
def retrieve_apikey(self, providerkey=None, token=None): """ Get an API key from a registration token retrieved in retrieve/token. The user must have approved your request first, or you will get an error response. The parameters are : - providerkey (required) : your provider API key. - token (required): the token returned from retrieve_token. This returns a dictionary such as: {'apikey': u'16b776682332cf11102b67d6db215821f2c233a3', 'code': u'200', 'remaining': u'999', 'resetdate': u'1299535575'} """ h = Https(API_DOMAIN) data = {'apikey': self.apikey} if providerkey is not None: data['providerkey'] = providerkey else: raise Exception("Provider Key is required for retrieving API key") if token is not None: data['token'] = token else: raise Exception("Token is required for retrieving API key.\ Call retrieve_token to request it.") h.request("GET", "/publicapi/retrieve/apikey?" + urlencode(data), headers=self.headers) request = h.getresponse() request_status = request.status if request_status == 200: dom = minidom.parseString(request.read()) code = dom.getElementsByTagName('prowl')[0].\ getElementsByTagName('success')[0].\ getAttribute('code') remaining = dom.getElementsByTagName('prowl')[0].\ getElementsByTagName('success')[0].\ getAttribute('remaining') resetdate = dom.getElementsByTagName('prowl')[0].\ getElementsByTagName('success')[0].\ getAttribute('resetdate') users_api_key = dom.getElementsByTagName('prowl')[0].\ getElementsByTagName('retrieve')[0].\ getAttribute('apikey') return dict(apikey=users_api_key, code=code, remaining=remaining, resetdate=resetdate) else: self._relay_error(request_status)
def _do_post(self, query, extra_headers=[]): """ Do a POST to the Institution. :param query: Body content to POST (OFX Query) :type query: str :param extra_headers: Extra headers to send with the request, as a list of (Name, Value) header 2-tuples. :type extra_headers: list :return: 2-tuple of (HTTPResponse, str response body) :rtype: tuple """ i = self.institution logging.debug('posting data to %s' % i.url) garbage, path = splittype(i.url) host, selector = splithost(path) try: h = HTTPSConnection(host, timeout=60) h.connect() except ssl.SSLError as ex: if (ex.reason == "UNSUPPORTED_PROTOCOL"): h = HTTPSConnection(host, timeout=60, context=ssl.SSLContext(ssl.PROTOCOL_TLSv1)) h.connect() else: raise # Discover requires a particular ordering of headers, so send the # request step by step. h.putrequest('POST', selector, skip_host=True, skip_accept_encoding=True) headers = [ ('Content-Type', 'application/x-ofx'), ('Host', host), ('Content-Length', len(query)), ('Connection', 'Keep-Alive') ] if self.accept: headers.append(('Accept', self.accept)) if self.user_agent: headers.append(('User-Agent', self.user_agent)) for ehname, ehval in extra_headers: headers.append((ehname, ehval)) logging.debug('---- request headers ----') for hname, hval in headers: logging.debug('%s: %s', hname, hval) h.putheader(hname, hval) logging.debug('---- request body (query) ----') logging.debug(query) h.endheaders(query.encode()) res = h.getresponse() response = res.read().decode('ascii', 'ignore') logging.debug('---- response ----') logging.debug(res.__dict__) logging.debug('Headers: %s', res.getheaders()) logging.debug(response) res.close() return res, response
def request(self, method, url, body=None): connection = HTTPSConnection('saucelabs.com') connection.request(method, url, body, headers=self.headers) response = connection.getresponse() json_data = response.read() connection.close() if response.status != 200: raise Exception('%s: %s.\nSauce Status NOT OK' % (response.status, response.reason)) return json_data
def call_pushover(self, msg): if not self.pushover: return token = self.pushover['token'] user = self.pushover['user'] if self.curl: subprocess.check_call( self.curl % curl_pushover.format(token=token, user=user, message=msg), shell=True ) else: conn = HTTPSConnection('api.pushover.net:443') conn.request( 'POST', '/1/messages.json', urlencode({'token': token, 'user': user, 'message': msg}), {'Content-type': 'application/x-www-form-urlencoded'} ) conn.getresponse()
def check_updated_certs(_address, _port, certhashlist, newhash=None, timeout=config.default_timeout, connect_timeout=config.connect_timeout, traversefunc=None): update_list = [] if None in [_address, _port]: logging.error("address or port empty") return None addr, _port = url_to_ipv6(_address, _port) cont = default_sslcont() con = HTTPSConnection(addr, _port, context=cont, timeout=connect_timeout) try: con.connect() except (ConnectionRefusedError, socket.timeout): if not traversefunc: logging.warning("Connection failed") return None # try_traverse does not work here, scnreqest creates loop con.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) con.sock.bind(('', 0)) traversefunc(("", con.sock.getsockname()[1])) con.sock.settimeout(connect_timeout) for count in range(0, config.traverse_retries): try: con.sock.connect((addr, _port)) break except Exception: pass else: logging.warning("traversal failed") return None con.sock.settimeout(timeout) oldhash = dhash(ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip()) if newhash and newhash != oldhash: return None oldsslcont = con.sock.context for _hash, _security in certhashlist: con.request("POST", "/usebroken/{hash}".format(hash=_hash), headers=cert_update_header) con.sock = con.sock.unwrap() con.sock = cont.wrap_socket(con.sock, server_side=False) con.sock.do_handshake() brokensslcert = ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip() con.sock = con.sock.unwrap() # without next line the connection would be unencrypted now con.sock = oldsslcont.wrap_socket(con.sock, server_side=False) # con.sock.do_handshake() ret = con.getresponse() if ret.status != 200: logging.info("checking cert failed, code: %s, reason: %s", ret.status, ret.reason) continue if con.sock and oldhash != dhash(ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip()): logging.error("certificate switch detected, stop checking") break if dhash(brokensslcert) == _hash: update_list.append((_hash, _security)) con.close() return update_list
def lookForAddress(loc): print("Google GEOCODE Query") conn = HTTPSConnection("maps.googleapis.com") parametres = "latlng="+loc.latlng()+"&sensor=false&key="+commons.googleAPIKey conn.request("GET", "/maps/api/geocode/xml?"+parametres) response = conn.getresponse() data = response.read() dom = parseString(data) for d in dom.getElementsByTagNameNS("*", 'formatted_address'): loc.desc = d.firstChild.data return
def feed(bot, update): url = HTTPSConnection("test.bop.rest") headers = { 'Authorization': "Token 233e7ef7888d82e098b3d63ca2a888d0e32a0eea" } url.request('GET', '/api/feed/', headers=headers) res = url.getresponse() data = res.read() data_json = json.loads(data) i = 1 result = '' for item in data_json[-10:]: result += str(i) + '|' + item['name'] + '|' + item['time'] + '\n' i += 1 update.message.reply_text(result)
def invalid_urls(urllist: list) -> List[str]: import ssl ssl._create_default_https_context = ssl._create_unverified_context bad_urls = [] for url in urllist: try: parsed = urlparse(url) conn = HTTPSConnection(host=parsed.netloc) conn.request("HEAD", parsed.path) res = conn.getresponse() if res.status in [404]: bad_urls.append(url) except HTTPException: bad_urls.append(url) return bad_urls
def fetch_file(self, server, path): "Fetch file using httplib module." print("downloading https://%s%s" % (server, path)) req = HTTPSConnection(server) req.putrequest('GET', path) req.putheader('Host', server) req.putheader('Accept', 'text/svg') req.endheaders() r1 = req.getresponse() data = r1.read().decode('utf-8') req.close() return data
def fetch(config, endpoint): jwt = makeJWT(config) conn = HTTPSConnection(config["host"]) headers = { 'authorization': "Bearer " + jwt, 'content-type': "application/json" } conn.request("GET", endpoint, headers=headers) res = conn.getresponse() data = res.read() return loads(data.decode("utf-8"))
def _get_device_properties(dsn: str, headers: dict, conn: HTTPSConnection): conn.request('GET', '/apiv1/dsns/{}/properties.json'.format(dsn), headers=headers) resp = conn.getresponse() if resp.status != 200: logging.error('Failed to get properties data from Hisense server: %r', resp) sys.exit(1) resp_data = resp.read() try: resp_data = gzip.decompress(resp_data) except OSError: pass # Not gzipped. return json.loads(resp_data)
def request(self, path, method, data=None): connection = HTTPSConnection(self.domain) try: base64_bytes = b64encode( ("%s:%s" % (self.username, self.password)).encode("ascii")).decode("ascii") headers = { 'Authorization': 'Basic %s' % base64_bytes, 'Content-Encoding': 'gzip' } connection.request(method, path, headers=headers, body=data) response = connection.getresponse() return loads(response.read().decode()) finally: connection.close()
def collect(self): connection = HTTPSConnection(self.ip_api_host) try: connection.connect() connection.request(GET_METHOD, self.ip_api_url) response = connection.getresponse() if response.code == 200: return CollectingResult( data=response.readline().decode(ENCODING)) else: return CollectingResult(sucess=False, data="0.0.0.0") except Exception as e: return CollectingResult(sucess=False, data=e.args[1]) finally: connection.close()
def getbody_or_die(host, url) -> str: conn = HTTPSConnection(host=host) try: print('GET {} {}'.format(host, url)) conn.request('GET', url=url) r = conn.getresponse() if r.getcode() < 200 or r.getcode() >= 300: sys.stderr.write( 'Error while sending a request to {}. code: {} reason: {}'. format(url, r.getcode(), r.reason)) sys.exit(5) return r.read().decode() finally: print('GET DONE {} {}'.format(host, url)) conn.close()
class GitHubContent: """ Get contents of a github url. May not be used presently. """ def __init__(self): self.conn = HTTPSConnection(DOMAIN) def fetch(self, url): self.conn.request('GET', url, headers=HEADERS) resp = self.conn.getresponse() if resp.status == 200: return b64decode(json.loads(resp.read())['content'])\ .decode("utf8") def repo_info(self, repo_s): url = REPO_INFO % (repo_s) self.conn.request('GET', url, headers=HEADERS) resp = self.conn.getresponse() if resp.status == 200: return json.loads(resp.read()) # repo.subscribers_count = data['watchers_count'] # repo.stargazers_count = data['watchers_count'] # repo.watchers_count = data['watchers_count'] def last_author(self, repo): """ :param repo: in format of {owner}/{project} :return: """ url = LAST_COMMIT % (repo) self.conn.request('GET', url, headers=HEADERS) resp = self.conn.getresponse() if resp.status == 200: author = json.loads(resp.read())[0]['commit']['author'] return author
def callapi(self, method, path, args): headers = {'User-Agent': USER_AGENT} if method == "POST": headers['Content-type'] = "application/x-www-form-urlencoded" http_handler = HTTPSConnection(API_SERVER) http_handler.request(method, path, urlencode(args), headers) resp = http_handler.getresponse() try: res = self._parse_reponse(resp.read()) except Exception as e: res = {'type': "pynmaerror", 'code': 600, 'message': str(e)} pass return res
def _do_get(self, data): if self.is_ssl: http_handler = HTTPSConnection(self.url) else: http_handler = HTTPConnection(self.url) get_path = self.path + '?' + data http_handler.request(CUSTOM_METHOD_GET, get_path, headers=self.headers) http_response = http_handler.getresponse() if http_response.status == 200: return True else: current_app.logger.info('Event Custom Notification Failed on GET method') raise Exception('Custom Notification Failed')
def get_token(api_key): connection = HTTPSConnection("auth.dfuse.io") connection.request('POST', '/v1/auth/issue', json.dumps({"api_key": api_key}), {'Content-type': 'application/json'}) response = connection.getresponse() if response.status != 200: raise Exception(" Status: %s reason: %s" % (response.status, response.reason)) token = json.loads(response.read().decode())['token'] connection.close() return token
def download_data(host, uri, https=True): count = RETRY_COUNT while count > 0: try: if https: conn = HTTPSConnection(host, timeout=10) else: conn = HTTPConnection(host, timeout=10) conn.request("GET", uri, headers={"User-Agent": "XMLHttpRequest"}) res = conn.getresponse().read() assert len(res > 5000) break except: count -= 1 return res
def send(self, type, text, raw): message_time = time.time() message_timestamp = time.ctime(message_time) if check_time_restriction(self.starttime, self.endtime): if self.suppress_timestamp == False: self.msg_to_send = text[:10000].encode( 'utf8') + " Message Sent at: " + message_timestamp else: self.msg_to_send = text[:10000].encode('utf8') self.event = NMA_EVENT.encode('utf8') self.content_type = NMA_CONTENT_TYPE notify_data = { 'application': self.app_name, 'description': self.msg_to_send, 'event': self.event, 'priority': self.priority, 'content-type': self.content_type, 'apikey': self.api_key } headers = {'User-Agent': NMA_USER_AGENT} headers['Content-type'] = NMA_HEADER_CONTENT_TYPE if sys.version_info >= (2, 7, 9): http_handler = HTTPSConnection( NMA_URL, context=ssl._create_unverified_context()) else: http_handler = HTTPSConnection(NMA_URL) http_handler.request(NMA_METHOD, NMA_PATH, urlencode(notify_data), headers) http_response = http_handler.getresponse() try: res = self._parse_response(http_response.read()) except Exception as e: res = { 'type': 'NMA Notify Error', 'code': 800, 'message': str(e) } current_app.logger.info( 'Event NotifyMyAndroid Notification Failed: {0}'.format( str(e))) raise Exception('NotifyMyAndroid Failed: {0}'.format(str(e)))
def get_authentication_token(email, password): connection = HTTPSConnection("www.snapfish.com") # submit=true and componentID=1395868004571 (URL parameters), and # "iwPreActions:" "submit" and "next": "https://www.snapfish.com/home" # (request headers) are all required outside of an email and password for # the login request to work. All login requests seem to use the same # component ID. Where said ID originates from and what exactly it # represents is a mystery for another day. connection.request( "POST", "/loginto?submit=true&componentID=1395868004571", urllib.parse.urlencode({ "iwPreActions": "submit", "next": "https://www.snapfish.com/home", "EmailAddress": email, "Password": password, }).encode(), { "Content-Type": "application/x-www-form-urlencoded", }, ) response = connection.getresponse() # A response status other than 302 indicates the login did not contain the # necessary information to fetch an authentication token or that the request # was unsuccessful (due to invalid credentials, network error, etc.). if not response.status == 302: raise RuntimeError( f"""error connecting to Snapfish when fetching authentication token (HTTP {response.status} {response.reason}).""" ) # The authentication token is not returned in the response body. Rather, it # exists in a Set-Cookie response header containing a value starting with # "oa2=sf_v1a". Said value holds additional, URL encoded information # separated by semicolons, where the token is the first element. Valid # authentication tokens in Snapfish endpoints are prefixed with # "Oauth sf_v1a", so the token contained in the header value must have its # "oa2=sf_v1a" prefix replaced accordingly. token = None for key, value in response.headers.items(): if key != "Set-Cookie" or not value.startswith("oa2=sf_v1a"): continue token = urllib.parse.unquote( value.split(";")[0].replace("oa2=sf_v1a", "OAuth sf_v1a")) return token
def _do_post(self, query, extra_headers=[]): """ Do a POST to the Institution. :param query: Body content to POST (OFX Query) :type query: str :param extra_headers: Extra headers to send with the request, as a list of (Name, Value) header 2-tuples. :type extra_headers: list :return: 2-tuple of (HTTPResponse, str response body) :rtype: tuple """ i = self.institution logging.debug('posting data to %s' % i.url) garbage, path = splittype(i.url) host, selector = splithost(path) h = HTTPSConnection(host, timeout=60) # Discover requires a particular ordering of headers, so send the # request step by step. h.putrequest('POST', selector, skip_host=True, skip_accept_encoding=True) headers = [ ('Content-Type', 'application/x-ofx'), ('Host', host), ('Content-Length', len(query)), ('Connection', 'Keep-Alive') ] if self.accept: headers.append(('Accept', self.accept)) if self.user_agent: headers.append(('User-Agent', self.user_agent)) for ehname, ehval in extra_headers: headers.append((ehname, ehval)) logging.debug('---- request headers ----') for hname, hval in headers: logging.debug('%s: %s', hname, hval) h.putheader(hname, hval) logging.debug('---- request body (query) ----') logging.debug(query) h.endheaders(query.encode()) res = h.getresponse() response = res.read().decode('ascii', 'ignore') logging.debug('---- response ----') logging.debug(res.__dict__) logging.debug('Headers: %s', res.getheaders()) logging.debug(response) res.close() return res, response
def authenticate(self): conn = HTTPSConnection("linalgo.eu.auth0.com") headers = {'content-type': "application/json"} payload = json.dumps({ 'client_id': self.id, 'client_secret': self.secret, 'audience': self.audience, 'grant_type': "client_credentials" }) conn.request("POST", "/oauth/token", payload, headers) res = conn.getresponse() data = res.read() data = json.loads(data.decode("utf-8")) self.access_token = data['access_token'] self.expires_in = data['expires_in'] self.token_type = data['token_type']
def download_response(self, request_url: str, max_retries: int = 3) -> str: # Establish a connection for _ in range(max_retries): conn = HTTPSConnection(self.base_url) conn.request('GET', request_url) response = conn.getresponse() body = response.read().decode() response_code = response.getcode() conn.close() if response_code != 200: continue return body return ''
def open(self, data): if data["scheme"] == "https": conn = HTTPSConnection(data["netloc"], timeout=self.Parent.Configs.timeout) else: conn = HTTPConnection(data["netloc"], timeout=self.Parent.Configs.timeout) try: conn.request("GET", data["path"], "", data["headers"]) except: return False try: response = conn.getresponse() output = {"status":response.status, "reason":response.reason, "data":response.read(), "headers":response.getheaders()} except: output = False conn.close() return output
def get_collections(token): connection = HTTPSConnection("assets.snapfish.com") connection.request( "GET", "/pict/v2/collection/monthIndex?limit=0&skip=0&projection=createDate,assetType,files,assetIdList,userTags,updateDate,systemTags", None, {"access_token": token}, ) response = connection.getresponse() if not response.status == 200: raise RuntimeError( f"error connecting to Snapfish when fetching collection and album information (HTTP {response.status} {response.reason})." ) return json.loads(response.read())["entityMap"]
def get_photos(token, album_id): connection = HTTPSConnection("assets.snapfish.com") connection.request( "GET", f"/pict/v2/collection/{album_id}/assets?assetType=PICTURE", None, {"access_token": token}, ) response = connection.getresponse() if not response.status == 200: raise RuntimeError( f"error connecting to Snapfish when fetching photo information (HTTP {response.status} {response.reason})." ) return json.loads(response.read())["entities"]
def post_analysis(): if request.method == 'POST': payload = { 'id': request.form.get('id'), 'ma_period': request.form.get('ma-period'), 'var_window': request.form.get('var-window'), 'mc_samples': request.form.get('mc-samples'), 'scalable_services': request.form.get('scalable-services'), 'parallel_resources': request.form.get('parallel-resources') } body = json.dumps(payload) c = HTTPSConnection(api_endpoint) c.request('POST', '/Prod/riskAnalyses', body) response = c.getresponse() new_analysis_id = response.msg.get('location') return redirect(url_for('get_analyses') + '/' + new_analysis_id)
def grab_page( self, url, binary=False, error='Received HTTP {0} error when gathering JSON data.\n'): domain, path = py3_url_split(url) conn = HTTPSConnection(domain, 443) conn.request('GET', path, headers=self.headers) resp = conn.getresponse() if resp.status == 200: return resp.read() if binary else json_to_array(resp.read()) else: stderr.write(error.format(resp.status)) stdout.write(resp.read().decode('iso-8859-1')) return ''
def lambda_handler(event, context): ''' ''' parsed = urlparse(os.environ['SLACK_URL']) conn = HTTPSConnection(parsed.hostname) for (subject, message) in summarize_messages(event): if subject == message: body = json.dumps(dict(text=subject)) else: body = json.dumps(dict(text=subject, attachments=[dict(text=message)])) conn.request('POST', parsed.path, body) resp = conn.getresponse() print('Sent:', body) print('HTTP {} from {}'.format(resp.status, parsed.hostname))
def sendRequest(request): conn = HTTPSConnection(request['headers']['host']) conn.request( request['method'], request['target'], json.dumps(request['body']).encode() if request['body'] else None, request['headers']) response = conn.getresponse() body = response.read().decode() return { 'protocol': httpVersion[response.version], 'status': response.status, 'reason': response.reason, 'headers': {k.lower(): v for k, v in dict(response.headers).items()}, 'body': json.loads(body) if body else None }
def request_handler(request=None): conn = HTTPSConnection("api.tfl.gov.uk") conn.request("GET", "/Line/Mode/dlr,elizabeth-line,overground,tube/Status") response = conn.getresponse() attachments = json.loads(response.read(), object_hook=object_hook) return ( json.dumps({ "response_type": "in_channel", "attachments": attachments, }), 200, { "Content-Type": "application/json", }, )
def _call(self, args): """Calls a remote method.""" if not isinstance(args, dict): raise ValueError("Args must be a dictionary") args['login'] = self.config['login'] args['password'] = self.config['password'] host = "lcab.sms-uslugi.ru" url = "/lcabApi/sendSms.php" values = urlencode(args) headers = {'Content-Type': 'application/x-www-form-urlencoded'} conn = HTTPSConnection(host) conn.request('POST', url, values, headers) response = conn.getresponse() data = response.read() return json.loads(data.decode('utf-8'))
def post_request(self, data, extra_path="jsonwsp", encoding="utf-8"): headers = { "Content-type": "application/json, charset=%s" % encoding, "Accept": "application/json" } if self.scheme.lower() == 'https': conn = HTTPSConnection(self.hostname, self.port) else: conn = HTTPConnection(self.hostname, self.port) req_path = self.path + '/' + extra_path conn.request("POST", req_path, data, headers) response = conn.getresponse() status, reason = response.status, response.reason resdata = response.read() conn.close() return status, reason, resdata
def post_locations(locations): request_url = '/admin/pages/' + options.page_id + '.json' body = generate_body(locations) conn = HTTPSConnection(host=(options.url_prefix + ".myshopify.com")) userAndPass = b64encode( (options.shopify_api_key + ':' + options.shopify_api_pass).encode('UTF-8')).decode('ascii') headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic %s' % userAndPass } conn.request('PUT', request_url, headers=headers, body=body) res = conn.getresponse() data = res.read() conn.close() print(data)
def pushover_handler(message): """ Send parsed message to Pushover """ logger.info('Received message' + json.dumps(message)) conn = HTTPSConnection("api.pushover.net:443") conn.request( "POST", "/1/messages.json", urlencode({ "token": pushover_token, "user": pushover_user, "message": message['text'], "sound": message['sound'], "priority": message['priority'], "title": message['title'] }), {"Content-type": "application/x-www-form-urlencoded"}) response = conn.getresponse() return response.status