Python urllib.urlopen() Examples

The following are code examples for showing how to use urllib.urlopen(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_userpass_with_spaces_inurl(self):
        self.fakehttp('Hello!')
        try:
            url = "http://a b:c [email protected]/"
            fakehttp_wrapper = httplib.HTTP._connection_class
            authorization = ("Authorization: Basic %s\r\n" %
                             b64encode('a b:c d'))
            fp = urllib.urlopen(url)
            # The authorization header must be in place
            self.assertIn(authorization, fakehttp_wrapper.buf)
            self.assertEqual(fp.readline(), "Hello!")
            self.assertEqual(fp.readline(), "")
            # the spaces are quoted in URL so no match
            self.assertNotEqual(fp.geturl(), url)
            self.assertEqual(fp.getcode(), 200)
        finally:
            self.unfakehttp() 
Example 2
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_main():
    import warnings
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
                                DeprecationWarning)
        test_support.run_unittest(
            urlopen_FileTests,
            urlopen_HttpTests,
            urlretrieve_FileTests,
            urlretrieve_HttpTests,
            ProxyTests,
            QuotingTests,
            UnquotingTests,
            urlencode_Tests,
            Pathname_Tests,
            Utility_Tests,
            URLopener_Tests,
            #FTPWrapperTests,
        ) 
Example 3
Project: push-button-get-candy   Author: channemann   File: PBGC.py    MIT License 6 votes vote down vote up
def getPredictionLoop():
	# Get eventual glucose from Loop via NS
	eventualGlucoseRequest = "api/v1/devicestatus.json"
	eventualGlucoseURL = nsURL + eventualGlucoseRequest
	eventualGlucoseResponse = urllib.urlopen(eventualGlucoseURL).read().decode('utf-8')
	eventualGlucoseData = json.loads(eventualGlucoseResponse)
	# I'm unsure how to better accomplish what is happening below; the correct device entry may not be the 0th or 1st entry in the returned array ... need to search for it?
	try:
	    eventualGlucose = eventualGlucoseData[0]["loop"]["predicted"]["values"][-1]
	    predictionStartTime = dateutil.parser.parse(eventualGlucoseData[0]["loop"]["predicted"]["startDate"])
	    predictionEndTime = predictionStartTime + datetime.timedelta(minutes=(5*(len(eventualGlucoseData[0]["loop"]["predicted"]["values"])-5)))
	except:
	    eventualGlucose = eventualGlucoseData[1]["loop"]["predicted"]["values"][-1]
	    predictionStartTime = dateutil.parser.parse(eventualGlucoseData[1]["loop"]["predicted"]["startDate"])
	    predictionEndTime = predictionStartTime + datetime.timedelta(minutes=(5*(len(eventualGlucoseData[1]["loop"]["predicted"]["values"])-5)))
	print("Eventual Glucose (Loop):      " + str(eventualGlucose) + " " + glucoseUnit + " at " + predictionEndTime.astimezone(tzlocal()).strftime("%-I:%M:%S %p on %A, %B %d, %Y"))
	print("                              ... predicted at " + predictionStartTime.astimezone(tzlocal()).strftime("%-I:%M:%S %p on %A, %B %d, %Y"))
	return eventualGlucose 
Example 4
Project: neos-classic-electrum-server   Author: neoscoin   File: blockchain_processor.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def bitcoind(self, method, params=[]):
        postdata = dumps({"method": method, 'params': params, 'id': 'jsonrpc'})
        while True:
            try:
                respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
            except:
                print_log("cannot reach bitcoind...")
                self.wait_on_bitcoind()
            else:
                r = loads(respdata)
                if r['error'] is not None:
                    if r['error'].get('code') == -28:
                        print_log("bitcoind still warming up...")
                        self.wait_on_bitcoind()
                        continue
                    raise BaseException(r['error'])
                break
        return r.get('result') 
Example 5
Project: Servo   Author: fpsw   File: note.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def send_sms_builtin(self, recipient, sender=None):
        """
        Sends SMS through built-in gateway
        """
        if not settings.SMS_HTTP_URL:
            raise ValueError(_('System is not configured for built-in SMS support.'))

        if sender is None:
            location = self.created_by.location
            sender = location.title

        data = urllib.urlencode({
            'username'  : settings.SMS_HTTP_USERNAME,
            'password'  : settings.SMS_HTTP_PASSWORD,
            'numberto'  : recipient.replace(' ', ''),
            'numberfrom': sender.encode(SMS_ENCODING),
            'message'   : self.body.encode(SMS_ENCODING),
        })

        from ssl import _create_unverified_context
        f = urllib.urlopen(settings.SMS_HTTP_URL, data, context=_create_unverified_context())
        return f.read() 
Example 6
Project: Servo   Author: fpsw   File: sms.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def send(self):
        pwhash = md5(self.conf['sms_http_password']).hexdigest()

        params = {
            'username'  : self.conf['sms_http_user'],
            'password'  : pwhash,
            'message'   : self.body,
            'from'      : self.sender,
            'to'        : self.recipient,
        }

        if self.msg:
            dlruri = settings.SERVO_URL + '/api/messages/?id={0}'.format(self.msg.code)
            params['notify_url'] = dlruri

        params = urllib.urlencode(params)
        r = urllib.urlopen(self.URL, params, context=_create_unverified_context()).read()

        if 'ERROR:' in r:
            raise ValueError(self.ERRORS.get(r, _('Unknown error (%s)') % r)) 
Example 7
Project: Servo   Author: fpsw   File: sms.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def send(self, note, number):
        conf = Configuration.conf()

        if not conf.get('sms_http_url'):
            raise ValueError(_("No SMS HTTP gateway defined"))

        params = urllib.urlencode({
            'username'  : conf['sms_http_user'],
            'password'  : conf['sms_http_password'],
            'text'      : note.body.encode('utf8'),
            'to'        : number
        })

        f = urllib.urlopen("%s?%s" % (conf['sms_http_url'], params),
                           context=_create_unverified_context())
        return f.read() 
Example 8
Project: Recipes   Author: Lasagne   File: cifar10.py    MIT License 6 votes vote down vote up
def download_dataset(path, source='https://www.cs.toronto.edu/~kriz/'
                                  'cifar-10-python.tar.gz'):
    """
    Downloads and extracts the dataset, if needed.
    """
    files = ['data_batch_%d' % (i + 1) for i in range(5)] + ['test_batch']
    for fn in files:
        if not os.path.exists(os.path.join(path, 'cifar-10-batches-py', fn)):
            break  # at least one file is missing
    else:
        return  # dataset is already complete

    print("Downloading and extracting %s into %s..." % (source, path))
    if sys.version_info[0] == 2:
        from urllib import urlopen
    else:
        from urllib.request import urlopen
    import tarfile
    if not os.path.exists(path):
        os.makedirs(path)
    u = urlopen(source)
    with tarfile.open(fileobj=u, mode='r|gz') as f:
        f.extractall(path=path)
    u.close() 
Example 9
Project: IPToCC   Author: roniemartinez   File: updater.py    MIT License 6 votes vote down vote up
def update_rir_database(rir_database_url):
    rir_database_path = os.path.join('iptocc', rir_database_url.split('/')[-1])
    try:
        if os.path.isfile(rir_database_path):
            hash_md5 = hashlib.md5()
            calculate_hash(hash_md5, rir_database_path)
            md5_text = urlopen(rir_database_url + '.md5').read().decode('utf-8')
            calculated_md5 = hash_md5.hexdigest()
            if not (calculated_md5 != md5_text[-33:-1] or calculated_md5 != md5_text[:32]):
                print("Updating RIR database: {}".format(rir_database_url))
                urlretrieve(rir_database_url, filename=rir_database_path)
                print("RIR database updated: {}".format(rir_database_url))
            else:
                print("RIR database is up-to-date: {}".format(rir_database_path))
        else:
            print("Downloading RIR database {}".format(rir_database_path))
            urlretrieve(rir_database_url, filename=rir_database_path)
            print("RIR database downloaded: {}".format(rir_database_url))
    except IOError:
        pass 
Example 10
Project: pyhanlp   Author: hankcs   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def hanlp_releases(cache=True):
    global HANLP_RELEASES
    if cache and HANLP_RELEASES:
        return HANLP_RELEASES
    # print('Request GitHub API')
    req = urllib.Request('http://nlp.hankcs.com/download.php?file=version')
    req.add_header('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36')
    if PY == 3:
        content = urllib.urlopen(req).read()
    else:
        content = urllib.urlopen(req).read()
    content = json.loads(content.decode())
    jar_version, jar_url, data_version, data_url = content
    meta = [(jar_version, jar_url, data_version, data_url)]
    HANLP_RELEASES = meta
    return meta 
Example 11
Project: jawfish   Author: war-and-code   File: jf-web.py    MIT License 6 votes vote down vote up
def run_simulation(self):
        global TARGET, ADDR, VULN_VAR, TIMEOUT, REQ_TOTAL,\
            METHOD, OTHER_VARIABLES
        tmp = OTHER_VARIABLES
        tmp[VULN_VAR] = self.genome
        try:
            if METHOD == 0:
                prep = urllib.urlencode(tmp)
                r = urllib.urlopen('http://%s/%s' % (TARGET, ADDR), data=prep, timeout=TIMEOUT)
            else:
                prep = urllib.urlencode(tmp)
                req = urllib.Request('http://%s/%s' % (TARGET, ADDR), data=prep)
                r = urllib.urlopen(req)
            REQ_TOTAL += 1
            self.m_text['text'] = r.get_data()
            self.m_text['url'] = r.get_full_url()
            self.m_text['status_code'] = r.getcode()
        except:
            pass
        return self.m_text 
Example 12
Project: show-me-the-code   Author: hacpai   File: main.py    GNU General Public License v2.0 6 votes vote down vote up
def print_zip_information():
    """Prompts the user for a zip code and
    prints the name and population of the corresponding town.
    """
    zip_code = raw_input( 'Please input a zip code: ' )
    url = 'http://www.uszip.com/zip/%s' % zip_code
    conn = urllib.urlopen( url )
    for line in conn.fp:
        line = line.strip()
        if 'Population' in line:
            print line
        if 'Logitude' in line:
            print line
        if 'Latitude' in line:
            print line

    conn.close() 
Example 13
Project: odorik   Author: nijel   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def post(self, path, args=None):
        """Perform GET request on the API."""
        args = self._fill_args(args)
        url = '{0}{1}'.format(self.url, path)
        request = urlopen(url, urlencode(args).encode('utf-8'))
        return request.read().decode('utf-8') 
Example 14
Project: odorik   Author: nijel   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self, path, args=None):
        """Perform GET request on the API."""
        args = self._fill_args(args)
        url = '{0}{1}?{2}'.format(
            self.url,
            path,
            urlencode(args)
        )
        request = urlopen(url)
        return request.read().decode('utf-8') 
Example 15
Project: pyblish-win   Author: pyblish   File: pyversioncheck.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _check1version(package, url, version, verbose=0):
    if verbose >= VERBOSE_EACHFILE:
        print '  Checking %s'%url
    try:
        fp = urllib.urlopen(url)
    except IOError, arg:
        if verbose >= VERBOSE_EACHFILE:
            print '    Cannot open:', arg
        return -1, None, None 
Example 16
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def testURLread(self):
        f = _open_with_retry(urllib.urlopen, "http://www.example.com/")
        x = f.read() 
Example 17
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def urlopen(self, *args):
        return _open_with_retry(urllib.urlopen, *args) 
Example 18
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        # Simple test expected to pass.
        open_url = self.urlopen("http://www.example.com/")
        for attr in ("read", "readline", "readlines", "fileno", "close",
                     "info", "geturl"):
            self.assertTrue(hasattr(open_url, attr), "object returned from "
                            "urlopen lacks the %s attribute" % attr)
        try:
            self.assertTrue(open_url.read(), "calling 'read' failed")
        finally:
            open_url.close() 
Example 19
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_info(self):
        # Test 'info'.
        open_url = self.urlopen("http://www.example.com/")
        try:
            info_obj = open_url.info()
        finally:
            open_url.close()
            self.assertIsInstance(info_obj, mimetools.Message,
                                  "object returned by 'info' is not an "
                                  "instance of mimetools.Message")
            self.assertEqual(info_obj.getsubtype(), "html") 
Example 20
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_geturl(self):
        # Make sure same URL as opened is returned by geturl.
        URL = "http://www.example.com/"
        open_url = self.urlopen(URL)
        try:
            gotten_url = open_url.geturl()
        finally:
            open_url.close()
        self.assertEqual(gotten_url, URL) 
Example 21
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_fileno(self):
        # Make sure fd returned by fileno is valid.
        open_url = self.urlopen("http://www.example.com/")
        fd = open_url.fileno()
        FILE = os.fdopen(fd)
        try:
            self.assertTrue(FILE.read(), "reading from file created using fd "
                                      "returned by fileno failed")
        finally:
            FILE.close() 
Example 22
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bad_address(self):
        # Make sure proper exception is raised when connecting to a bogus
        # address.
        bogus_domain = "sadflkjsasf.i.nvali.d"
        try:
            socket.gethostbyname(bogus_domain)
        except socket.gaierror:
            pass
        else:
            # This happens with some overzealous DNS providers such as OpenDNS
            self.skipTest("%r should not resolve for test to work" % bogus_domain)
        self.assertRaises(IOError,
                          # SF patch 809915:  In Sep 2003, VeriSign started
                          # highjacking invalid .com and .net addresses to
                          # boost traffic to their own site.  This test
                          # started failing then.  One hopes the .invalid
                          # domain will be spared to serve its defined
                          # purpose.
                          # urllib.urlopen, "http://www.sadflkjsasadf.com/")
                          urllib.urlopen, "http://sadflkjsasf.i.nvali.d/") 
Example 23
Project: pyblish-win   Author: pyblish   File: test_urllibnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_context_argument(self):
        context = ssl.create_default_context(cafile=CERT_selfsigned_pythontestdotnet)
        response = urllib.urlopen("https://self-signed.pythontest.net", context=context)
        self.assertIn("Python", response.read()) 
Example 24
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def fakehttp(self, fakedata):
        class FakeSocket(StringIO.StringIO):

            def sendall(self, data):
                FakeHTTPConnection.buf = data

            def makefile(self, *args, **kwds):
                return self

            def read(self, amt=None):
                if self.closed:
                    return ""
                return StringIO.StringIO.read(self, amt)

            def readline(self, length=None):
                if self.closed:
                    return ""
                return StringIO.StringIO.readline(self, length)

        class FakeHTTPConnection(httplib.HTTPConnection):

            # buffer to store data for verification in urlopen tests.
            buf = ""

            def connect(self):
                self.sock = FakeSocket(fakedata)

        assert httplib.HTTP._connection_class == httplib.HTTPConnection

        httplib.HTTP._connection_class = FakeHTTPConnection 
Example 25
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        """Setup of a temp file to use for testing"""
        self.text = "test_urllib: %s\n" % self.__class__.__name__
        FILE = file(test_support.TESTFN, 'wb')
        try:
            FILE.write(self.text)
        finally:
            FILE.close()
        self.pathname = test_support.TESTFN
        self.returned_obj = urllib.urlopen("file:%s" % self.pathname) 
Example 26
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_interface(self):
        # Make sure object returned by urlopen() has the specified methods
        for attr in ("read", "readline", "readlines", "fileno",
                     "close", "info", "geturl", "getcode", "__iter__"):
            self.assertTrue(hasattr(self.returned_obj, attr),
                         "object returned by urlopen() lacks %s attribute" %
                         attr) 
Example 27
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_relativelocalfile(self):
        self.assertRaises(ValueError,urllib.urlopen,'./' + self.pathname) 
Example 28
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_read(self):
        self.fakehttp('Hello!')
        try:
            fp = urllib.urlopen("http://python.org/")
            self.assertEqual(fp.readline(), 'Hello!')
            self.assertEqual(fp.readline(), '')
            self.assertEqual(fp.geturl(), 'http://python.org/')
            self.assertEqual(fp.getcode(), 200)
        finally:
            self.unfakehttp() 
Example 29
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_read_bogus(self):
        # urlopen() should raise IOError for many error codes.
        self.fakehttp('''HTTP/1.1 401 Authentication Required
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Type: text/html; charset=iso-8859-1
''')
        try:
            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
        finally:
            self.unfakehttp() 
Example 30
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_invalid_redirect(self):
        # urlopen() should raise IOError for many error codes.
        self.fakehttp("""HTTP/1.1 302 Found
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Location: file:README
Connection: close
Content-Type: text/html; charset=iso-8859-1
""")
        try:
            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
        finally:
            self.unfakehttp() 
Example 31
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_empty_socket(self):
        # urlopen() raises IOError if the underlying socket does not send any
        # data. (#1680230)
        self.fakehttp('')
        try:
            self.assertRaises(IOError, urllib.urlopen, 'http://something')
        finally:
            self.unfakehttp() 
Example 32
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_missing_localfile(self):
        self.assertRaises(IOError, urllib.urlopen,
                'file://localhost/a/missing/file.py')
        fd, tmp_file = tempfile.mkstemp()
        tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
        self.assertTrue(os.path.exists(tmp_file))
        try:
            fp = urllib.urlopen(tmp_fileurl)
            fp.close()
        finally:
            os.close(fd)
            os.unlink(tmp_file)

        self.assertFalse(os.path.exists(tmp_file))
        self.assertRaises(IOError, urllib.urlopen, tmp_fileurl) 
Example 33
Project: pyblish-win   Author: pyblish   File: test_urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_ftp_nonexisting(self):
        self.assertRaises(IOError, urllib.urlopen,
                'ftp://localhost/not/existing/file.py') 
Example 34
Project: annots   Author: Infernion   File: travis_pypi_setup.py    MIT License 5 votes vote down vote up
def fetch_public_key(repo):
    """Download RSA public key Travis will use for this repo.

    Travis API docs: http://docs.travis-ci.com/api/#repository-keys
    """
    keyurl = 'https://api.travis-ci.org/repos/{0}/key'.format(repo)
    data = json.loads(urlopen(keyurl).read().decode())
    if 'key' not in data:
        errmsg = "Could not find public key for repo: {}.\n".format(repo)
        errmsg += "Have you already added your GitHub repo to Travis?"
        raise ValueError(errmsg)
    return data['key'] 
Example 35
Project: CSL_Hamburg_Noise   Author: CityScope   File: CityScopeTable.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, address, table_flipped):
        self.address = address
        self.table_flipped = table_flipped

        try:
            self.result = json.load(urllib.urlopen(self.address))
            self.start_cell_origin = (Point(self.result['header']['spatial']['longitude'], self.result['header']['spatial']['latitude']))
        # use local debugging table as fallback
        except:
            print("cannot read from CityIO server. Using local fallback json")
            cwd = os.path.dirname(os.path.abspath(__file__))
            debug_json_path = os.path.abspath(cwd + "/__debugging_virtual_table.json")
            self.result = json.load(open(debug_json_path))
            self.start_cell_origin = (Point(self.result['header']['spatial']['longitude'], self.result['header']['spatial']['latitude']))

        self.table_rotation = self.result['header']['spatial']['rotation']  # TODO can the table rotation be different form the cell rotation??
        self.table_cell_size = self.result['header']['spatial']['cellSize']
        self.table_row_count = self.result['header']['spatial']['nrows']
        self.table_column_count = self.result['header']['spatial']['ncols']
        self.table_mapping = self.result['header']['mapping']['type']
        self.table_cell_content = self.result['header']['block']
        # todo enter mapping to get street id

        # get projections from config.ini
        config = configparser.ConfigParser()
        file_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
        config.read(file_path + '/config.ini')
        self.origin_epsg = config['CITY_SCOPE']['GLOBAL_EPSG']
        self.local_epsg = config['CITY_SCOPE']['LOCAL_EPSG'] 
Example 36
Project: CSL_Hamburg_Noise   Author: CityScope   File: grid_listener.py    GNU General Public License v3.0 5 votes vote down vote up
def check_for_grid_changes(table_url, last_id):
    try:
        hash_id = json.load(urllib.urlopen(table_url + '/meta/hashes/grid'))
    except:
        print('Cant access cityIO at',table_url)
        hash_id = 0 # TODO: shouldn't this be last_id? in case, connection fails during later iterations?

    grid_changed = (hash_id != last_id)

    return grid_changed, hash_id 
Example 37
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: mklabels.py    MIT License 5 votes vote down vote up
def generate(url):
    parts = ['''\
"""

    webencodings.labels
    ~~~~~~~~~~~~~~~~~~~

    Map encoding labels to their name.

    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.

"""

# XXX Do not edit!
# This file is automatically generated by mklabels.py

LABELS = {
''']
    labels = [
        (repr(assert_lower(label)).lstrip('u'),
         repr(encoding['name']).lstrip('u'))
        for category in json.loads(urlopen(url).read().decode('ascii'))
        for encoding in category['encodings']
        for label in encoding['labels']]
    max_len = max(len(label) for label, name in labels)
    parts.extend(
        '    %s:%s %s,\n' % (label, ' ' * (max_len - len(label)), name)
        for label, name in labels)
    parts.append('}')
    return ''.join(parts) 
Example 38
Project: push-button-get-candy   Author: channemann   File: PBGC.py    MIT License 5 votes vote down vote up
def getGlucoseNS():
# Get most recent glucose from NS
	currentGlucoseRequest = "api/v1/entries.json?count=1"
	currentGlucoseURL = nsURL + currentGlucoseRequest
	currentGlucoseResponse = urllib.urlopen(currentGlucoseURL).read().decode('utf-8')
	currentGlucoseData = json.loads(currentGlucoseResponse)
	currentGlucose = currentGlucoseData[0]["sgv"]
	currentGlucoseTime = dateutil.parser.parse(currentGlucoseData[0]["dateString"])
	print("Current Glucose (Nightscout): " + str(currentGlucose) + " " + glucoseUnit + " at " + currentGlucoseTime.astimezone(tzlocal()).strftime("%-I:%M:%S %p on %A, %B %d, %Y"))
	# Calculate staleness of the data ...
	ageCurrentGlucose = round((datetime.datetime.now().replace(tzinfo=tzlocal()) - currentGlucoseTime).total_seconds())
	print("                              ... {} seconds ago".format(ageCurrentGlucose))
	return currentGlucose 
Example 39
Project: flasky   Author: RoseOu   File: _luabuiltins.py    MIT License 5 votes vote down vote up
def get_newest_version():
        f = urllib.urlopen('http://www.lua.org/manual/')
        r = re.compile(r'^<A HREF="(\d\.\d)/">Lua \1</A>')
        for line in f:
            m = r.match(line)
            if m is not None:
                return m.groups()[0] 
Example 40
Project: flasky   Author: RoseOu   File: _luabuiltins.py    MIT License 5 votes vote down vote up
def get_lua_functions(version):
        f = urllib.urlopen('http://www.lua.org/manual/%s/' % version)
        r = re.compile(r'^<A HREF="manual.html#pdf-(.+)">\1</A>')
        functions = []
        for line in f:
            m = r.match(line)
            if m is not None:
                functions.append(m.groups()[0])
        return functions 
Example 41
Project: flasky   Author: RoseOu   File: _sourcemodbuiltins.py    MIT License 5 votes vote down vote up
def get_version():
        f = urllib.urlopen('http://docs.sourcemod.net/api/index.php')
        r = re.compile(r'SourceMod v\.<b>([\d\.]+)</td>')
        for line in f:
            m = r.search(line)
            if m is not None:
                return m.groups()[0] 
Example 42
Project: flasky   Author: RoseOu   File: _sourcemodbuiltins.py    MIT License 5 votes vote down vote up
def get_sm_functions():
        f = urllib.urlopen('http://docs.sourcemod.net/api/SMfuncs.js')
        r = re.compile(r'SMfunctions\[\d+\] = Array \("(?:public )?([^,]+)",".+"\);')
        functions = []
        for line in f:
            m = r.match(line)
            if m is not None:
                functions.append(m.groups()[0])
        return functions 
Example 43
Project: flasky   Author: RoseOu   File: _postgres_builtins.py    MIT License 5 votes vote down vote up
def fetch(url):
    return urllib.urlopen(url) 
Example 44
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: vm_utils.py    Apache License 2.0 5 votes vote down vote up
def _get_rrd(server, vm_uuid):
    """Return the VM RRD XML as a string."""
    try:
        xml = urllib.urlopen("%s://%s:%[email protected]%s/vm_rrd?uuid=%s" % (
            server[0],
            CONF.xenserver.connection_username,
            CONF.xenserver.connection_password,
            server[1],
            vm_uuid))
        return xml.read()
    except IOError:
        LOG.exception(_LE('Unable to obtain RRD XML for VM %(vm_uuid)s with '
                          'server details: %(server)s.'),
                      {'vm_uuid': vm_uuid, 'server': server})
        return None 
Example 45
Project: sic   Author: Yanixos   File: mklabels.py    GNU General Public License v3.0 5 votes vote down vote up
def generate(url):
    parts = ['''\
"""

    webencodings.labels
    ~~~~~~~~~~~~~~~~~~~

    Map encoding labels to their name.

    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.

"""

# XXX Do not edit!
# This file is automatically generated by mklabels.py

LABELS = {
''']
    labels = [
        (repr(assert_lower(label)).lstrip('u'),
         repr(encoding['name']).lstrip('u'))
        for category in json.loads(urlopen(url).read().decode('ascii'))
        for encoding in category['encodings']
        for label in encoding['labels']]
    max_len = max(len(label) for label, name in labels)
    parts.extend(
        '    %s:%s %s,\n' % (label, ' ' * (max_len - len(label)), name)
        for label, name in labels)
    parts.append('}')
    return ''.join(parts) 
Example 46
Project: neos-classic-electrum-server   Author: neoscoin   File: blockchain_processor.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def getfullblock(self, block_hash):
        block = self.bitcoind('getblock', [block_hash])

        rawtxreq = []
        i = 0
        for txid in block['tx']:
            rawtxreq.append({
                "method": "getrawtransaction",
                "params": [txid],
                "id": i,
            })
            i += 1

        postdata = dumps(rawtxreq)
        try:
            respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
        except:
            logger.error("bitcoind error (getfullblock)",exc_info=True)
            self.shared.stop()

        r = loads(respdata)
        rawtxdata = []
        for ir in r:
            if ir['error'] is not None:
                self.shared.stop()
                print_log("Error: make sure you run bitcoind with txindex=1; use -reindex if needed.")
                raise BaseException(ir['error'])
            rawtxdata.append(ir['result'])
        block['tx'] = rawtxdata
        return block 
Example 47
Project: unrardll-debian   Author: norbusan   File: ci.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def download(url):
    i = 5
    while i > 0:
        i -= 1
        try:
            return urlopen(url).read()
        except Exception:
            if i <= 0:
                raise
            print('Download failed, retrying...')
            sys.stdout.flush()
            time.sleep(1) 
Example 48
Project: trenitapy   Author: sgs00   File: travis_pypi_setup.py    ISC License 5 votes vote down vote up
def fetch_public_key(repo):
    """Download RSA public key Travis will use for this repo.

    Travis API docs: http://docs.travis-ci.com/api/#repository-keys
    """
    keyurl = 'https://api.travis-ci.org/repos/{0}/key'.format(repo)
    data = json.loads(urlopen(keyurl).read().decode())
    if 'key' not in data:
        errmsg = "Could not find public key for repo: {}.\n".format(repo)
        errmsg += "Have you already added your GitHub repo to Travis?"
        raise ValueError(errmsg)
    return data['key'] 
Example 49
Project: ANN   Author: waynezv   File: baidu_spy.py    MIT License 5 votes vote down vote up
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html 
Example 50
Project: Servo   Author: fpsw   File: sms.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def send(self):
        """
        Sends SMS through SMS Jazz Gateway
        """
        conf = Configuration.conf()

        if not conf.get('sms_http_sender'):
            raise ValueError(_("SMS sender name not configured"))

        body = self.note.body.encode('utf-8')
        sender = conf.get('sms_http_sender')
        pwhash = md5(conf['sms_http_password']).hexdigest()
        checksum = md5(body + self.recipient.encode('ascii') + pwhash).hexdigest()

        params = {
            'username'  : conf['sms_http_user'],
            'password'  : pwhash,
            'message'   : body,
            'sender'    : sender.encode('ascii', 'replace'),
            'receiver'  : self.recipient,
            'charset'   : 'UTF8',
            'checksum'  : checksum,
        }

        if self.msg:
            dlruri = '/api/messages/?id={0}&status=%status%'.format(self.msg.code)
            dlruri = settings.SERVO_URL + dlruri
            params['dlruri'] = dlruri

        params = urllib.urlencode(params)
        r = urllib.urlopen(self.URL, params, context=_create_unverified_context()).read()

        if not '1:OK' in r:
            raise ValueError(_('Failed to send message to %s') % self.recipient)