def wait_for_spark_workers(num_of_expected_workers, timeout): """ This queries the spark master and checks for the expected number of workers """ start_time = time.time() while True: opener = build_opener(HTTPHandler) request = Request("http://{0}:7080".format(CASSANDRA_IP)) request.get_method = lambda: 'GET' connection = opener.open(request) match = re.search('Alive Workers:.*(\d+)</li>', connection.read().decode('utf-8')) num_workers = int(match.group(1)) if num_workers == num_of_expected_workers: match = True break elif time.time() - start_time > timeout: match = True break time.sleep(1) return match
def __call__(self, url): # Extracted from above madness # TODO: add mode alike to 'relaxed' where we would not # care about content-disposition filename # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way request = Request(url) # No traffic compression since we do not know how to identify # exactly either it has to be decompressed # request.add_header('Accept-encoding', 'gzip,deflate') # # TODO: think about stamping etc -- we seems to be redoing # what git-annex does for us already... not really r = retry_urlopen(request) try: r_info = r.info() r_stamp = get_url_response_stamp(url, r_info) return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url) finally: r.close()
def get_github_email(access_token): """Get real email from GitHub""" request = Request('https://api.github.com/user/emails') request.timeout = 1.0 request.add_header('User-Agent', USER_AGENT) request.add_header( 'Authorization', 'token {0}'.format(access_token) ) handle = urlopen(request) data = json.loads(handle.read().decode('utf-8')) email = None for entry in data: # Skip not verified ones if not entry['verified']: continue email = entry['email'] if entry['primary']: break return email
def wgetUrl(query): try: target = "http://www.tv3.ie/player/assets/php/search.php" values = {'queryString': query, 'limit': 20} headers = {} headers[ 'User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3' headers['DNT'] = '1' headers['Referer'] = 'http://www.tv3.ie/3player/' headers[ 'Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8' data = urlencode(values) req = Request(target, data, headers) response = urlopen(req) html = str(response.read()) response.close() return html except (Exception) as exception: print('wgetUrl: Error retrieving URL ', exception) return ''
def upload_disk(self, fileItem, lease, host): """ Upload an individual disk. Passes the file handle of the disk directly to the urlopen request. """ filename = self.get_disk(fileItem, lease) if filename is None: return deviceUrl = self.get_device_url(fileItem, lease) url = deviceUrl.url.replace('*', host) headers = {'Content-length': os.path.getsize(filename)} if hasattr(ssl, '_create_unverified_context'): sslContext = ssl._create_unverified_context() else: sslContext = None print("Uploading disk: %s" % filename) self.handle = FileHandle(filename) req = Request(url, self.handle, headers) urlopen(req, context=sslContext)
def auth_complete(self, *args, **kwargs): """Completes loging process, must return user instance""" self.process_error(self.data) params = self.auth_complete_params(self.validate_state()) request = Request( self.ACCESS_TOKEN_URL, data=urlencode(params), headers=self.auth_headers() ) try: response = json.loads(dsa_urlopen(request).read()) except HTTPError as e: logger.exception( "plugins.auth.error", extra={"class": type(self), "status_code": e.code, "response": e.read()[:128]}, ) raise AuthUnknownError(self) except (ValueError, KeyError): raise AuthUnknownError(self) self.process_error(response) return self.do_auth(response["access_token"], response=response, *args, **kwargs)
def render(self): client = self.context.client(self.request) uri, headers, body = client.sign(self.context.request_token_uri) req = Request(uri) for h, v in headers: req.add_header(h, v) req.add_data(body) try: res = urlopen(req).read() # should be doing a post access = self.context.process(res) # Redirect with parameter: oauth_token (optional) data = dict(oauth_token=access.parms['oauth_token']) url = "{}?{}".format(self.context.auth_uri, urlencode(data)) self.redirect(url) return '<a class="autolink" href="%s">Please visit: %s</a>' % ( str(url), str(url)) except HTTPError as e: print "Error while opening {}: {}".format(str(req), str(e)) self.error = str(e)
def validate_url(url, lint_ctx, user_agent=None): is_valid = True if user_agent: req = Request(url, headers={"User-Agent": user_agent}) else: req = url try: handle = urlopen(req) handle.read(100) except HTTPError as e: if e.code == 429: # too many requests pass else: is_valid = False lint_ctx.error("HTTP Error %s accessing %s" % (e.code, url)) except URLError as e: is_valid = False lint_ctx.error("URL Error %s accessing %s" % (str(e), url)) if is_valid: lint_ctx.info("URL OK %s" % url)
def test_nonexistent_resources(self): # Create a server with a placeholder handler so we don't fall back # to serving local files httpd = mozhttpd.MozHttpd(port=0) httpd.start(block=False) server_port = httpd.httpd.server_port # GET: Return 404 for non-existent endpoint exception_thrown = False try: urlopen(self.get_url('/api/resource/', server_port, None)) except HTTPError as e: self.assertEqual(e.code, 404) exception_thrown = True self.assertTrue(exception_thrown) # POST: POST should also return 404 exception_thrown = False try: urlopen( self.get_url('/api/resource/', server_port, None), data=json.dumps({}), ) except HTTPError as e: self.assertEqual(e.code, 404) exception_thrown = True self.assertTrue(exception_thrown) # DEL: DEL should also return 404 exception_thrown = False try: opener = build_opener(HTTPHandler) request = Request(self.get_url('/api/resource/', server_port, None)) request.get_method = lambda: 'DEL' opener.open(request) except HTTPError: self.assertEqual(e.code, 404) exception_thrown = True self.assertTrue(exception_thrown)
def create_credential(self, id, username, private_key=''): '''Create a new Jenkins credential :param id: id of the credential, ``str`` :param username: Name of the credential, ``str`` :param private_key: private key, ``str`` ''' if self.credential_exists(id): return None inner_params = { "": "0", "credentials": { "scope": 'GLOBAL', "id": id, "username": username, "password": "", "privateKeySource": { "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource", "privateKey": private_key, }, "description": "jenkins credentials with private key", "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" } } params = {'json': inner_params} data = urlencode(params) url = self._build_url(CREATE_CREDENTIAL) request = Request(url, data, URLENCODE_HEADERS) self.jenkins_open(request) self.assert_credential_exists(id)
def _notify_emby(self, message, host=None, emby_apikey=None): """Handles notifying Emby host via HTTP API Returns: Returns True for no issue or False if there was an error """ # fill in omitted parameters if not host: host = app.EMBY_HOST if not emby_apikey: emby_apikey = app.EMBY_APIKEY url = 'http://%s/emby/Notifications/Admin' % host values = { 'Name': 'Medusa', 'Description': message, 'ImageUrl': app.LOGO_URL } data = json.dumps(values) try: req = Request(url, data) req.add_header('X-MediaBrowser-Token', emby_apikey) req.add_header('Content-Type', 'application/json') response = urlopen(req) result = response.read() response.close() log.debug(u'EMBY: HTTP response: {0}', result.replace('\n', '')) return True except (URLError, IOError) as error: log.warning( u'EMBY: Warning: Unable to contact Emby at {url}: {error}', { 'url': url, 'error': ex(error) }) return False
def test_nonexistent_resources(httpd_no_urlhandlers): # GET: Return 404 for non-existent endpoint with pytest.raises(HTTPError) as excinfo: urlopen(httpd_url(httpd_no_urlhandlers, "/api/resource/")) assert excinfo.value.code == 404 # POST: POST should also return 404 with pytest.raises(HTTPError) as excinfo: urlopen( httpd_url(httpd_no_urlhandlers, "/api/resource/"), data=ensure_binary(json.dumps({})), ) assert excinfo.value.code == 404 # DEL: DEL should also return 404 opener = build_opener(HTTPHandler) request = Request(httpd_url(httpd_no_urlhandlers, "/api/resource/")) request.get_method = lambda: "DEL" with pytest.raises(HTTPError) as excinfo: opener.open(request) assert excinfo.value.code == 404
def DownloadSetting(link): req = Request(link) req.add_header('User-Agent', 'VAS') response = urlopen(req) newlink = response.read() response.close() Setting = open(Directory + '/Settings/tmp/listE2.zip', 'wb') Setting.write(newlink) Setting.close() if os.path.exists(Directory + '/Settings/tmp/listE2.zip'): os.system('mkdir ' + Directory + '/Settings/tmp/listE2_unzip') try: os.system('unzip -q ' + Directory + '/Settings/tmp/listE2.zip -d ' + Directory + '/Settings/tmp/listE2_unzip') except: print("ERROR unzip listE2.zip") if not os.path.exists(Directory + '/Settings/tmp/setting'): os.system('mkdir ' + Directory + '/Settings/tmp/setting') try: os.system('unzip -q ' + Directory + '/Settings/tmp/listE2_unzip/*.zip -d ' + Directory + '/Settings/tmp/setting') except: print("ERROR unzip %s.zip", name) return False
def invoke_storlet_on_copy_dest(self): # No COPY in swiftclient. Using urllib instead... url = '%s/%s/%s' % (self.url, self.container, self.storlet_file) headers = { 'X-Auth-Token': self.token, 'X-Run-Storlet': self.storlet_name, 'X-Object-Meta-Name': 'thumbnail', 'Destination': '%s/gen_thumb_on_copy_.jpg' % self.container } headers.update(self.additional_headers) req = Request(url, headers=headers) req.get_method = lambda: 'COPY' conn = urlopen(req, timeout=10) status = conn.getcode() self.assertIn(status, [201, 202]) headers = c.head_object(self.url, self.token, self.container, 'gen_thumb_on_copy_.jpg') self.assertLess(int(headers['content-length']), 1087318) self.assertEqual('thumbnail', headers['x-object-meta-name']) self.assertTrue('x-object-meta-x-timestamp' not in headers) self.assertTrue('x-timestamp' in headers)
def has_internet(): """ Test if Internet is available. Failure of connecting to the site "http://www.sagemath.org" within a second is regarded as internet being not available. EXAMPLES:: sage: from sage.doctest.external import has_internet sage: has_internet() # optional -- internet True """ from six.moves import urllib from six.moves.urllib.request import Request, urlopen req = Request("http://www.sagemath.org", headers={"User-Agent": "sage-doctest"}) try: urlopen(req, timeout=1) return True except urllib.error.URLError: return False
def get_feed_html(feed_url): try: req = Request(feed_url) req.add_header( "User-Agent", ("Webring Pelican plugin/{} " + "+https://github.com/pelican/pelican-plugins" ).format(WEBRING_VERSION), ) return urlopen(req).read().decode("utf-8") except URLError as e: if hasattr(e, "reason"): warning( "webring plugin: failed to connect to feed url (%s).", feed_url, e.reason, ) if hasattr(e, "code"): warning("webring plugin: server returned %s error (%s).", e.code, feed_url) except ValueError as e: warning("webring plugin: wrong url provided (%s).", e)
def send(self, data): """ Send an https request. """ try: headers = { 'User-Agent': 'BluepPay Python Library/' + self.RELEASE_VERSION, 'Content-Type': 'application/x-www-form-urlencoded' } req = Request(self.url, data, headers=headers) r = urlopen(req) response = "" response = r.geturl() if self.api == 'bp10emu' else r.read() return response except HTTPError as e: if re.match("https://secure.bluepay.com/interfaces/wlcatch", e.geturl()): response = e.geturl() return response #return e.read() return "ERROR"
def get_plugin_info(self, name, depth=2): """Get an installed plugin information on this Master. This method retrieves information about a speicifc plugin. The passed in plugin name (short or long) must be an exact match. :param name: Name (short or long) of plugin, ``str`` :param depth: JSON depth, ``int`` :returns: a specific plugin ``dict`` Example:: >>> j = Jenkins() >>> info = j.get_plugin_info("Gearman Plugin") >>> print(info) {u'backupVersion': None, u'version': u'0.0.4', u'deleted': False, u'supportsDynamicLoad': u'MAYBE', u'hasUpdate': True, u'enabled': True, u'pinned': False, u'downgradable': False, u'dependencies': [], u'url': u'http://wiki.jenkins-ci.org/display/JENKINS/Gearman+Plugin', u'longName': u'Gearman Plugin', u'active': True, u'shortName': u'gearman-plugin', u'bundled': False} """ try: plugins_info = json.loads(self.jenkins_open( Request(self.server + PLUGIN_INFO % self._get_encoded_params(locals())))) for plugin in plugins_info['plugins']: if plugin['longName'] == name or plugin['shortName'] == name: return plugin except HTTPError: raise JenkinsException("Error communicating with server[%s]" % self.server) except BadStatusLine: raise JenkinsException("Error communicating with server[%s]" % self.server) except ValueError: raise JenkinsException("Could not parse JSON info for server[%s]" % self.server)
def process_network_request(self, url, data, timeout): if data: for key in data.keys(): if isinstance(data[key], six.text_type): data[key] = data[key].encode('utf-8') req_data = urlencode(data).encode('utf-8') else: req_data = None req = Request(url, req_data) try: response = urlopen(req, timeout=timeout) body = response.read() code = response.getcode() except HTTPError as ex: code = ex.code body = ex.fp.read() return { 'code': code, 'body': body, 'url': url, }
def oembed(url, max_width=None): # Find provider provider = get_oembed_provider(url) if provider is None: raise EmbedNotFoundException # Work out params params = {'url': url, 'format': 'json'} if max_width: params['maxwidth'] = max_width # Perform request request = Request(provider + '?' + urlencode(params)) request.add_header('User-agent', 'Mozilla/5.0') try: r = urllib_request.urlopen(request) except URLError: raise EmbedNotFoundException oembed = json.loads(r.read().decode('utf-8')) # Convert photos into HTML if oembed['type'] == 'photo': html = '<img src="%s" />' % (oembed['url'], ) else: html = oembed.get('html') # Return embed as a dict return { 'title': oembed['title'] if 'title' in oembed else '', 'author_name': oembed['author_name'] if 'author_name' in oembed else '', 'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '', 'type': oembed['type'], 'thumbnail_url': oembed.get('thumbnail_url'), 'width': oembed.get('width'), 'height': oembed.get('height'), 'html': html, }
def get_wf_node_log(self, name, number, node): """ Get build log for execution node. :param name: Job name, ``str`` :param name: Build number, ``int`` :param name: Execution node number, ``int`` :returns: Execution node build log, ``dict`` """ folder_url, short_name = self.client._get_job_folder(name) number = int(number) try: response = self.client.jenkins_open( Request(self.client._build_url(WF_NODE_LOG, locals()))) if response: return json.loads(response) else: raise jenkins.JenkinsException( 'job[%s] number[%d] does not exist' % (name, number)) except HTTPError: raise jenkins.JenkinsException( 'job[%s] number[%d] does not exist' % (name, number))
def _query_api(self, clip): # Adapted from SpeechRecognition source code, modified to get text # onsets flac_data = clip.get_flac_data( convert_rate=None if clip.sample_rate >= 16000 else 16000, convert_width=None if clip.sample_width >= 2 else 2) model = "{0}_BroadbandModel".format("en-US") url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?{0}".format( urlencode({ "profanity_filter": "false", "continuous": "true", "model": model, "timestamps": "true", "inactivity_timeout": -1, })) request = Request(url, data=flac_data, headers={ "Content-Type": "audio/x-flac", "X-Watson-Learning-Opt-Out": "true", }) return self._send_request(request)
def use_auth_cookie(self): assert (self.username and self.baseurl), 'Please provide jenkins url, username '\ 'and password to get the session ID cookie.' login_url = 'j_acegi_security_check' jenkins_url = '{0}/{1}'.format(self.baseurl, login_url) data = urlencode({ 'j_username': self.username, 'j_password': self.password }).encode("utf-8") class SmartRedirectHandler(HTTPRedirectHandler): def extract_cookie(self, setcookie): # Extracts the last cookie. # Example of set-cookie value for python2 # ('set-cookie', 'JSESSIONID.30blah=blahblahblah;Path=/; # HttpOnly, JSESSIONID.30ablah=blahblah;Path=/;HttpOnly'), return setcookie.split(',')[-1].split(';')[0].strip('\n\r ') def http_error_302(self, req, fp, code, msg, headers): # Jenkins can send several Set-Cookie values sometimes # The valid one is the last one for header, value in headers.items(): if header.lower() == 'set-cookie': cookie = self.extract_cookie(value) req.headers['Cookie'] = cookie result = HTTPRedirectHandler.http_error_302( self, req, fp, code, msg, headers) result.orig_status = code result.orig_headers = headers result.cookie = cookie return result request = Request(jenkins_url, data) opener = build_opener(SmartRedirectHandler()) res = opener.open(request) Requester.AUTH_COOKIE = res.cookie
def edx_login(url, headers, username, password): """ Log in user into the openedx website. """ logging.info('Logging into Open edX site: %s', url) post_data = urlencode({ 'email': username, 'password': password, 'remember': False }).encode('utf-8') request = Request(url, post_data, headers) try: response = urlopen(request) except HTTPError as e: logging.info('Error, cannot login: %s', e) return {'success': False} resp = json.loads(response.read().decode('utf-8')) return resp
def upload(self, filename): if not self.cdash_upload_url: return # Compute md5 checksum for the contents of this file. md5sum = checksum(hashlib.md5, filename, block_size=8192) opener = build_opener(HTTPHandler) with open(filename, 'rb') as f: params_dict = { 'build': self.buildname, 'site': self.site, 'stamp': self.buildstamp, 'MD5': md5sum, } encoded_params = urlencode(params_dict) url = "{0}&{1}".format(self.cdash_upload_url, encoded_params) request = Request(url, data=f) request.add_header('Content-Type', 'text/xml') request.add_header('Content-Length', os.path.getsize(filename)) if self.authtoken: request.add_header('Authorization', 'Bearer {0}'.format(self.authtoken)) try: # By default, urllib2 only support GET and POST. # CDash needs expects this file to be uploaded via PUT. request.get_method = lambda: 'PUT' response = opener.open(request) if self.current_package_name not in self.buildIds: resp_value = response.read() if isinstance(resp_value, bytes): resp_value = resp_value.decode('utf-8') match = self.buildid_regexp.search(resp_value) if match: buildid = match.group(1) self.buildIds[self.current_package_name] = buildid except Exception as e: print("Upload to CDash failed: {0}".format(e))
def downloadThumbnail(self, thumbUrl): if thumbUrl is not None: thumbID = thumbUrl.rsplit("/", 1)[1] thumbFile = None if not thumbUrl.startswith("http://"): thumbUrl = "%s%s" % (MAIN_PAGE, thumbUrl) try: req = Request(thumbUrl) url_handle = urlopen2(req) headers = url_handle.info() contentType = headers.getheader("content-type") except: contentType = None if contentType: if 'image/jpeg' in contentType: thumbFile = "/tmp/" + thumbID + ".jpg" elif 'image/gif' in contentType: thumbID = None # thumbFile = "/tmp/" + thumbID + ".gif" elif 'image/png' in contentType: thumbFile = "/tmp/" + thumbID + ".png" else: print("[ZDF Mediathek] Unknown thumbnail content-type:", contentType) if thumbFile is not None: if (os_path.exists(thumbFile) == True): #already downloaded self.downloadThumbnailCallback(None, thumbFile, thumbID) else: if self.png_cache.get(thumbID, None) is None: downloadPage(six.ensure_binary(thumbUrl), thumbFile).addCallback( self.downloadThumbnailCallback, thumbFile, thumbID).addErrback( self.downloadThumbnailError, thumbID) else: self.updateEntry(thumbID, thumbFile)
def _impl(): import warnings from six.moves.urllib.request import urlopen, Request import six try: r = Request( 'http://emma-project.org/versions.json', headers={ 'User-Agent': 'PyEMMA-{emma_version}-Py-{python_version}-{platform}-{addr}' .format(emma_version=current, python_version=platform.python_version(), platform=platform.platform(terse=True), addr=uuid.getnode()) } if not testing else {}) with closing(urlopen(r, timeout=30)) as response: args = {'encoding': 'ascii'} if six.PY3 else {} payload = str(response.read(), **args) # py3: encoding ascii versions = json.loads(payload) latest_json = tuple(filter(lambda x: x['latest'], versions))[0]['version'] latest = parse(latest_json) if parse(current) < latest: warnings.warn( "You are not using the latest release of PyEMMA." " Latest is {latest}, you have {current}.".format( latest=latest, current=current), category=UserWarning) if sys.version_info[0] < 3: warnings.warn( "Python 2.7 usage is deprecated. " "Future versions of PyEMMA will not support it. " "Please upgrade your Python installation.", category=UserWarning) except Exception: import logging logging.getLogger('pyemma').debug("error during version check", exc_info=True)
def Suite(self, succes): """ Après le téléchargement passe à la suite """ # Vérifie que le fichier est bien entier : tailleFichierDest = os.path.getsize(self.parent.fichierDest+ "/" + self.parent.nomFichier) tailleFichierOrigin = self.parent.tailleFichier if tailleFichierDest != tailleFichierOrigin : succes = False # Téléchargement terminé if succes == True : # On attribue un ID unique qui permet de compter les téléchargements IDfichier = FonctionsPerso.GetIDfichier() if len(IDfichier) > 7 : id = IDfichier[-7:] else : id = "" # On envoie le signal pour le compteur de téléchargement if "linux" in sys.platform : typeFichier = "linux" else: typeFichier = "windows" try : versionFichier = self.parent.versionFichier fichier = "%s-%s-%s" % (typeFichier, versionFichier, id) req = Request("http://www.noethys.com/fichiers/telechargement.cgi?fichier=%s" % fichier) handle = urlopen(req) except : pass # Si téléchargement complet, on passe à la page de fin de téléchargement sleep(1) # Attend 2 secondes avant de continuer self.parent.Active_page("page_fin_telechargement") else: # Vidage du rep Updates FonctionsPerso.VideRepertoireUpdates(forcer=True) # Le téléchargement n'est pas complet, demande à l'utilisateur de recommencer self.label_introduction.SetLabel(_(u"Le téléchargement n'est pas complet. Voulez-vous recommencer ?")) self.bouton_ok.Show(True) self.Layout()
def get_job_name(self, name): '''Return the name of a job using the API. That is roughly an identity method which can be used to quickly verify a job exist or is accessible without causing too much stress on the server side. :param name: Job name, ``str`` :returns: Name of job or None ''' try: response = self.jenkins_open( Request(self.server + JOB_NAME % self._get_encoded_params(locals()))) except NotFoundException: return None else: actual = json.loads(response)['name'] if actual != name: raise JenkinsException( 'Jenkins returned an unexpected job name %s ' '(expected: %s)' % (actual, name)) return actual
def _download_all_metadata(url): """ Downloads the json file that contains all of the metadata for a specific file type (read: audio files, benchmark files, or trained models) that is on the EFZ server. This is retrieved from one of following three URLs (which are stored in nussl.constants): NUSSL_EFZ_AUDIO_METADATA_URL, NUSSL_EFZ_BENCHMARK_METADATA_URL, or NUSSL_EFZ_MODEL_METADATA_URL. Args: url (str): URL for the EFZ server that has metadata. One of these three: NUSSL_EFZ_AUDIO_METADATA_URL, NUSSL_EFZ_BENCHMARK_METADATA_URL, or NUSSL_EFZ_MODEL_METADATA_URL. Returns: (list): List of dicts with metadata for the desired file type. """ request = Request(url) # Make sure to get the newest data request.add_header('Pragma', 'no-cache') request.add_header('Cache-Control', 'max-age=0') response = urlopen(request) return json.loads(response.read())