Python urllib.urlencode() Examples

The following are code examples for showing how to use urllib.urlencode(). 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: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def http_sync_post(url, params_dict=None):
    site_prefix = '/'+Options['site_name'] if Options['site_name'] else ''
    if url == site_prefix+'/_proxy':
        return sdproxy.sheetAction(params_dict)

    http_client = tornado.httpclient.HTTPClient()
    if params_dict:
        body = urllib.urlencode(params_dict)
        response = http_client.fetch(url, method='POST', headers=None, body=body)
    else:
        response = http_client.fetch(url, method='GET', headers=None, body=body)
    if response.error:
        raise Exception('ERROR in accessing URL %s: %s' % (url, excp))
    # Successful return
    result = response.read()
    try:
        result = json.loads(result)
    except Exception, excp:
        result = {'result': 'error', 'error': 'Error in http_sync_post: result='+str(result)+': '+str(excp)} 
Example 2
Project: google_streetview   Author: rrwen   File: api.py    MIT License 6 votes vote down vote up
def __init__(
    self,
    params,
    site_api='https://maps.googleapis.com/maps/api/streetview',
    site_metadata='https://maps.googleapis.com/maps/api/streetview/metadata'):
    
    # (params) Set default params
    defaults = {
      'size': '640x640'
    }
    for i in range(len(params)):
      for k in defaults:
        if k not in params[i]:
          params[i][k] = defaults[k]
    self.params = params
    
    # (image) Create image api links from parameters
    self.links = [site_api + '?' + urlencode(p) for p in params]
    
    # (metadata) Create metadata api links and data from parameters
    self.metadata_links = [site_metadata + '?' + urlencode(p) for p in params]
    self.metadata = [requests.get(url, stream=True).json() for url in self.metadata_links] 
Example 3
Project: core   Author: lifemapper   File: gbifquery.py    GNU General Public License v3.0 6 votes vote down vote up
def _getGBIFOccurrenceQuery(self, taxonKey, offset=0, limit=1):
      """
      @note: Add 'name' to queryParams
      """
      # Simple taxa query
      if taxonKey is None:
         raise Exception('Must provide taxonKey')
      queryParams = GBIF.QUERY_PARAMS[GBIF.OCCURRENCE_SERVICE].copy()
      queryParams[GBIF.REQUEST_TAXON_KEY] = taxonKey
      queryParams['offset'] = offset 
      queryParams['limit'] = limit
      
      url = '%s/%s/%s'% (GBIF.REST_URL, GBIF.OCCURRENCE_SERVICE, GBIF.SEARCH_COMMAND)
#       filterString = urllib.urlencode(queryParams)
#       if filterString:
#          url += '?%s' % filterString
      url = self._assembleUrl(url, queryParams)
      return url
   
# ............................................... 
Example 4
Project: sqliv   Author: the-robot   File: reverseip.py    GNU General Public License v3.0 6 votes vote down vote up
def reverseip(url):
    """return domains from given the same server"""

    # get only domain name
    url = urlparse(url).netloc if urlparse(url).netloc != '' else urlparse(url).path.split("/")[0]

    source = "http://domains.yougetsignal.com/domains.php"
    useragent = useragents.get()
    contenttype = "application/x-www-form-urlencoded; charset=UTF-8"

    # POST method
    opener = urllib2.build_opener(
        urllib2.HTTPHandler(), urllib2.HTTPSHandler())
    data = urllib.urlencode([('remoteAddress', url), ('key', '')])

    request = urllib2.Request(source, data)
    request.add_header("Content-type", contenttype)
    request.add_header("User-Agent", useragent)

    try:
        result = urllib2.urlopen(request).read()

    except urllib2.HTTPError, e:
        print >> sys.stderr, "[{}] HTTP error".format(e.code) 
Example 5
Project: sqliv   Author: the-robot   File: bing.py    GNU General Public License v3.0 6 votes vote down vote up
def search(self, query, stop=100):
        '''
        :type query : str
        :param query: Query for search
        
        :type stop  : int
        :param stop : Last result to retrieve.

        :rtype: list
        '''
 
        links = []
        start = 1

        for page in range(int(round(int(stop), -1)) / 10):
            URL = (self.bingsearch % (urllib.urlencode({'q': query}))) + '&first=' + str(start)

            html   = self.get_page(URL)
            result = self.parse_links(html)

            [links.append(_) for _ in result if _ not in links]

            start = start + 10

        return links 
Example 6
Project: EDMC-Canonn   Author: canonn-science   File: fssreports.py    GNU General Public License v3.0 6 votes vote down vote up
def gSubmitAXCZ(self, payload):
        p = payload.copy()
        p["x"], p["y"], p["z"] = Systems.edsmGetSystem(payload.get("systemName"))
        if p.get("isBeta"):
            p["isBeta"] = 'Y'
        else:
            p["isBeta"] = 'N'

        p["rawJson"] = json.dumps(payload.get("rawJson"), ensure_ascii=False).encode('utf8')

        url = "https://us-central1-canonn-api-236217.cloudfunctions.net/submitAXCZ"
        debug("gSubmitAXCZ {}".format(p.get("systemName")))

        getstr = "{}?{}".format(url, urllib.urlencode(p))

        debug("gsubmit {}".format(getstr))
        r = requests.get(getstr)

        if not r.status_code == requests.codes.ok:
            error(getstr)
            error(r.status_code) 
Example 7
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_read_write_util.py    Apache License 2.0 6 votes vote down vote up
def test_ipv6_host_read(self):
        ipv6_host = 'fd8c:215d:178e:c51e:200:c9ff:fed1:584c'
        port = 7443
        folder = 'tmp/fake.txt'
        # NOTE(sdague): the VMwareHTTPReadFile makes implicit http
        # call via requests during construction, block that from
        # happening here in the test.
        with mock.patch.object(read_write_util.VMwareHTTPReadFile,
                               '_create_read_connection'):
            reader = read_write_util.VMwareHTTPReadFile(ipv6_host,
                                                        port,
                                                        'fake_dc',
                                                        'fake_ds',
                                                        dict(),
                                                        folder)
            param_list = {"dcPath": 'fake_dc', "dsName": 'fake_ds'}
            base_url = 'https://[%s]:%s/folder/%s' % (ipv6_host, port, folder)
            base_url += '?' + urllib.urlencode(param_list)
            self.assertEqual(base_url, reader._base_url) 
Example 8
Project: sic   Author: Yanixos   File: request.py    GNU General Public License v3.0 6 votes vote down vote up
def request_encode_url(self, method, url, fields=None, headers=None,
                           **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if headers is None:
            headers = self.headers

        extra_kw = {'headers': headers}
        extra_kw.update(urlopen_kw)

        if fields:
            url += '?' + urlencode(fields)

        return self.urlopen(method, url, **extra_kw) 
Example 9
Project: txkoji   Author: ktdreyer   File: connection.py    MIT License 6 votes vote down vote up
def _authenticated_path(self):
        """
        Get the path of our XML-RPC endpoint with session auth params added.

        For example:
          /kojihub?session-id=123456&session-key=1234-asdf&callnum=0

        If we're making an authenticated request, we must add these session
        parameters to the hub's XML-RPC endpoint.

        :return: a path suitable for twisted.web.xmlrpc.Proxy
        """
        basepath = self.proxy.path.decode().split('?')[0]
        params = urlencode({'session-id': self.session_id,
                            'session-key': self.session_key,
                            'callnum': self.callnum})
        result = '%s?%s' % (basepath, params)
        return result.encode('utf-8') 
Example 10
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 11
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 12
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 13
Project: AshsSDK   Author: thehappydinoa   File: request.py    MIT License 6 votes vote down vote up
def request_encode_url(self, method, url, fields=None, headers=None,
                           **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if headers is None:
            headers = self.headers

        extra_kw = {'headers': headers}
        extra_kw.update(urlopen_kw)

        if fields:
            url += '?' + urlencode(fields)

        return self.urlopen(method, url, **extra_kw) 
Example 14
Project: zufang   Author: facert   File: wxbot.py    MIT License 6 votes vote down vote up
def sync_check(self):
        params = {
            'r': int(time.time()),
            'sid': self.sid,
            'uin': self.uin,
            'skey': self.skey,
            'deviceid': self.device_id,
            'synckey': self.sync_key_str,
            '_': int(time.time()),
        }
        url = 'https://' + self.sync_host + '/cgi-bin/mmwebwx-bin/synccheck?' + urllib.urlencode(params)
        try:
            r = self.session.get(url, timeout=60)
            r.encoding = 'utf-8'
            data = r.text
            pm = re.search(r'window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}', data)
            retcode = pm.group(1)
            selector = pm.group(2)
            return [retcode, selector]
        except:
            return [-1, -1] 
Example 15
Project: Paradrop   Author: ParadropLabs   File: http.py    Apache License 2.0 6 votes vote down vote up
def urlEncodeParams(data):
    """
    Return data URL-encoded.

    This function specifically handles None and boolean values
    to convert them to JSON-friendly strings (e.g. None -> 'null').
    """
    copy = dict()
    for key, value in six.iteritems(data):
        if value is None:
            copy[key] = 'null'
        elif isinstance(value, bool):
            copy[key] = json.dumps(value)
        else:
            copy[key] = value
    return urllib.urlencode(copy, doseq=True) 
Example 16
Project: pytracts   Author: rmorlok   File: to_url_test.py    Apache License 2.0 6 votes vote down vote up
def testProtourlencodeUnrecognizedField(self):
        """Test that unrecognized fields are saved and can be accessed."""

        class MyMessage(messages.Message):
            number = messages.IntegerField()

        decoded = to_url.decode_message(MyMessage,
                                                self.unexpected_tag_message)
        self.assertEquals(1, len(decoded.all_unrecognized_fields()))
        self.assertEquals('unexpected', decoded.all_unrecognized_fields()[0])
        # Unknown values set to a list of however many values had that name.
        self.assertEquals((['whatever'], messages.Variant.STRING),
                          decoded.get_unrecognized_field_info('unexpected'))

        repeated_unknown = urllib.urlencode([('repeated', 400),
                                             ('repeated', 'test'),
                                             ('repeated', '123.456')])
        decoded2 = to_url.decode_message(MyMessage, repeated_unknown)
        self.assertEquals((['400', 'test', '123.456'], messages.Variant.STRING),
                          decoded2.get_unrecognized_field_info('repeated')) 
Example 17
Project: Kython   Author: kiyadesu   File: CSDN.py    Apache License 2.0 6 votes vote down vote up
def login(self,username,password):

        print '| start loging in'
        # prepare for login
        lt, execution = parse_html(self.RequestInOne(self.login_url).read())
        post_data = urllib.urlencode({
            'username' : username,
            'password' : password,
            'lt' : lt,
            'execution' : execution,
            '_eventId' : 'submit'
        })
        response = self.RequestInOne(req_url=self.login_url,post_data=post_data)
        if response.read().find('该参数可以理解成') != -1:
            print '| login failed'
            return False

        return True 
Example 18
Project: Kython   Author: kiyadesu   File: CSDN.py    Apache License 2.0 6 votes vote down vote up
def publish_article(self,article_path):
        title,content,markdowncontent = format_file(article_path)
        print '| start publishing:' + title
        post_data = urllib.urlencode({
            'title' : title,
            'markdowncontent' : content,
            'content' : markdowncontent,
            'id' : '',
            'tags' : '',
            'description' : content[:100],
            'status' : '0',
            'level' : '0',
            'categories' : '',
            'channel' :	'1',
            'type' : 'original',
            'articleedittype' :	'1'
        })

        response = self.RequestInOne(req_url=self.publish_article_url,post_data=post_data)
        print '| return :' + response.read()[:-2] 
Example 19
Project: httpninja   Author: irsdl   File: web_request_socket.py    Apache License 2.0 6 votes vote down vote up
def _setParams(self):
        parsedURL = urlparse.urlparse(self.url)
        # setting the path
        if self.useAbsolutePath == True:
            self._path = self.url
        else:
            self._path = parsedURL.path
            self.qs = parsedURL.query

        if self._path == '':
            self._path = '/'

        # fix the body if it is in dict format
        if isinstance(self.body,dict):
            self.body = urllib.urlencode(self.body)

        # set other necessary parameters
        self.targetName = parsedURL.netloc
        self.targetPort = parsedURL.port
        self.targetProtocol = (parsedURL.scheme).lower()
        if self.targetProtocol == 'https':
            self.isSSL = True
            if self.targetPort == None: self.targetPort = 443
        elif self.targetPort == None:
            self.targetPort = 80 
Example 20
Project: httpninja   Author: irsdl   File: web_request_socket.py    Apache License 2.0 6 votes vote down vote up
def _setParams(self):
        parsedURL = urlparse.urlparse(self.url)
        # setting the path
        if self.useAbsolutePath == True:
            self._path = self.url
        else:
            self._path = parsedURL.path
            self.qs = parsedURL.query

        if self._path == '':
            self._path = '/'

        # fix the body if it is in dict format
        if isinstance(self.body,dict):
            self.body = urllib.urlencode(self.body)

        # set other necessary parameters
        self.targetName = parsedURL.hostname
        self.targetPort = parsedURL.port
        self.targetProtocol = (parsedURL.scheme).lower()
        if self.targetProtocol == 'https':
            self.isSSL = True
            if self.targetPort == None: self.targetPort = 443
        elif self.targetPort == None:
            self.targetPort = 80 
Example 21
Project: openhatch   Author: campbe13   File: models.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def google_query_url(project_name, **kwargs):
    extra_data = {
        u'num': 1000,  # 1000 is the maximum we can fetch
        u'can': GoogleTrackerModel.BUG_STATUS_OPEN,
        u'colspec': ' '.join((
            'ID',
            'Status',
            'Priority',
            'Owner',
            'Summary',
            'Stars',
            'Opened',
            'Closed',
            'Reporter',
            'Cc',
            'Difficulty',
            'Modified',
            'Type',
        )),
    }
    extra_data.update(kwargs)

    base = 'https://code.google.com/p/{project}/issues/csv'.format(project=project_name)
    return '{url}?{params}'.format(url=base, params=http.urlencode(extra_data)) 
Example 22
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 23
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 24
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def emit(self, record):
        """
        Emit a record.

        Send the record to the Web server as a percent-encoded dictionary
        """
        try:
            import httplib, urllib
            host = self.host
            h = httplib.HTTP(host)
            url = self.url
            data = urllib.urlencode(self.mapLogRecord(record))
            if self.method == "GET":
                if (url.find('?') >= 0):
                    sep = '&'
                else:
                    sep = '?'
                url = url + "%c%s" % (sep, data)
            h.putrequest(self.method, url)
            # support multiple hosts on one IP address...
            # need to strip optional :port from host, if present
            i = host.find(":")
            if i >= 0:
                host = host[:i]
            h.putheader("Host", host)
            if self.method == "POST":
                h.putheader("Content-type",
                            "application/x-www-form-urlencoded")
                h.putheader("Content-length", str(len(data)))
            h.endheaders(data if self.method == "POST" else None)
            h.getreply()    #can't do anything with the result
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record) 
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 help_inputtype(self, given, test_type):
        """Helper method for testing different input types.

        'given' must lead to only the pairs:
            * 1st, 1
            * 2nd, 2
            * 3rd, 3

        Test cannot assume anything about order.  Docs make no guarantee and
        have possible dictionary input.

        """
        expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
        result = urllib.urlencode(given)
        for expected in expect_somewhere:
            self.assertIn(expected, result,
                         "testing %s: %s not found in %s" %
                         (test_type, expected, result))
        self.assertEqual(result.count('&'), 2,
                         "testing %s: expected 2 '&'s; got %s" %
                         (test_type, result.count('&')))
        amp_location = result.index('&')
        on_amp_left = result[amp_location - 1]
        on_amp_right = result[amp_location + 1]
        self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
                     "testing %s: '&' not located in proper place in %s" %
                     (test_type, result))
        self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
                         "testing %s: "
                         "unexpected number of characters: %s != %s" %
                         (test_type, len(result), (5 * 3) + 2)) 
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_quoting(self):
        # Make sure keys and values are quoted using quote_plus()
        given = {"&":"="}
        expect = "%s=%s" % (hexescape('&'), hexescape('='))
        result = urllib.urlencode(given)
        self.assertEqual(expect, result)
        given = {"key name":"A bunch of pluses"}
        expect = "key+name=A+bunch+of+pluses"
        result = urllib.urlencode(given)
        self.assertEqual(expect, result) 
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_doseq(self):
        # Test that passing True for 'doseq' parameter works correctly
        given = {'sequence':['1', '2', '3']}
        expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3']))
        result = urllib.urlencode(given)
        self.assertEqual(expect, result)
        result = urllib.urlencode(given, True)
        for value in given["sequence"]:
            expect = "sequence=%s" % value
            self.assertIn(expect, result)
        self.assertEqual(result.count('&'), 2,
                         "Expected 2 '&'s, got %s" % result.count('&')) 
Example 28
Project: pyblish-win   Author: pyblish   File: test_httpservers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_post(self):
        params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
        headers = {'Content-type' : 'application/x-www-form-urlencoded'}
        res = self.request('/cgi-bin/file2.py', 'POST', params, headers)

        self.assertEqual(res.read(), '1, python, 123456\n') 
Example 29
Project: pyEntrezId   Author: lwgray   File: Conversion.py    MIT License 5 votes vote down vote up
def __init__(self, email):
        """Must Include Email"""
        self.params = {}
        self.email = email
        self.params['tool'] = 'PyEntrez'
        if re.match(r"[^@][email protected][^@]+\.[^@]+", self.email):
            pass
        else:
            raise ValueError("Enter a valid Email Address")
        self.params["email"] = email
        self.options = urlencode(self.params, doseq=True)
        return 
Example 30
Project: World-Bank-Python   Author: dpfens   File: api.py    MIT License 5 votes vote down vote up
def _request(url, **kwargs):
    headers = kwargs.get('headers', {})
    parameters = kwargs.get('parameters', {})
    data = kwargs.get('data', {})

    parameters.setdefault('format', 'json')

    encoded_parameters = urlencode(parameters)
    if encoded_parameters:
        url = '%s?%s' % (url, encoded_parameters)

    logger.debug(url)
    request = Request(url)
    for key, value in headers.items():
        request.add_header(key, value)

    for key, value in data.items():
        request.add_data(key, value)

    try:
        response = urlopen(request)
    except Exception as e:
        logger.error(e)
        raise
    raw_response_data = response.read().decode("utf-8")
    logger.debug(raw_response_data)
    response_data = json.loads(raw_response_data)
    return response_data 
Example 31
Project: azure-iot-rest   Author: jongio   File: module-twin.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 32
Project: azure-iot-rest   Author: jongio   File: modules.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 33
Project: azure-iot-rest   Author: jongio   File: device-conf.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key.encode('utf-8'), sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 34
Project: azure-iot-rest   Author: jongio   File: file-upload.py    MIT License 5 votes vote down vote up
def get_iot_hub_sas_token(uri, key, policy_name, expiry=3600):
    ttl = time() + expiry
    sign_key = "%s\n%d" % ((quote_plus(uri)), int(ttl))
    signature = b64encode(HMAC(b64decode(key), sign_key, sha256).digest())

    rawtoken = {
        'sr' :  uri,
        'sig': signature,
        'se' : str(int(ttl))
    }

    if policy_name is not None:
        rawtoken['skn'] = policy_name

    return 'SharedAccessSignature ' + urlencode(rawtoken) 
Example 35
Project: slidoc   Author: mitotic   File: sdprint.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def http_get(url, params_dict):
    try:
        return urllib2.urlopen(url + ('?'+urllib.urlencode(params_dict) if params_dict else '')).read()
    except Exception, excp:
        sys.exit('ERROR in accessing GET URL %s: %s' % (url, excp)) 
Example 36
Project: slidoc   Author: mitotic   File: sdprint.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def http_post(url, params_dict):
    data = urllib.urlencode(params_dict)
    req = urllib2.Request(url, data)
    try:
        response = urllib2.urlopen(req)
    except Exception, excp:
        sys.exit('ERROR in accessing POST URL %s: %s' % (url, excp)) 
Example 37
Project: slidoc   Author: mitotic   File: sdstream.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def twitter_request(twitter_config, path, get_args={}, post_args=None,
                    streaming_callback=None, connect_timeout=20.0, timeout=1200.0,
                    path_prefix="https://api.twitter.com/1.1"):
    app_token = twitter_config['consumer_token']
    access_token = twitter_config.get('access_token')
    url = path_prefix + path + ".json"
    method = "POST" if post_args is not None else "GET"
    query_args = get_args.copy()
    if access_token:
        all_args = get_args.copy()
        all_args.update(post_args or {})
        consumer_token = dict(key=app_token["consumer_key"], secret=app_token["consumer_secret"])
        oauth = oauth_request_parameters(consumer_token, url, access_token, all_args, method=method)
        query_args.update(oauth)

    if query_args: url += "?" + urllib.urlencode(query_args)
    post_data = urllib.urlencode(post_args) if post_args is not None else None
    headers = {"Connection": "keep-alive"} if streaming_callback else None
    http_request = tornado.httpclient.HTTPRequest(str(url), method,
                                                  body=post_data,
                                                  headers=headers,
                                                  user_agent='UserStream',
                                                  connect_timeout=connect_timeout,
                                                  request_timeout=timeout,
                                                  streaming_callback=streaming_callback)
    return http_request 
Example 38
Project: slidoc   Author: mitotic   File: sliauth.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def http_post(url, params_dict=None, add_size_info=False):
    req = urllib2.Request(url, urllib.urlencode(params_dict)) if params_dict else urllib2.Request(url)
    try:
        response = urllib2.urlopen(req)
    except Exception, excp:
        raise Exception('ERROR in accessing URL %s: %s' % (url, excp)) 
Example 39
Project: plugin.video.lynda   Author: davejm   File: lynda_api.py    GNU General Public License v2.0 5 votes vote down vote up
def _get(self, endpoint, params=None, new_headers=[]):
        url = self.API_HOST + endpoint
        if params:
            urlencoded_params = urllib.urlencode(params)
            url_with_params = url + "?" + urlencoded_params
            headers = self._headers(url_with_params)
        else:
            headers = self._headers(url)
        # Override/add headers
        for h in new_headers:
            headers[h] = new_headers[h]

        resp = self._s.get(url, params=params, headers=headers)
        return resp 
Example 40
Project: plugin.video.lynda   Author: davejm   File: lynda_api.py    GNU General Public License v2.0 5 votes vote down vote up
def _post(self, endpoint, data, new_headers=[]):
        url = self.API_HOST + endpoint

        headers = self._headers(url)
        # Override/add headers
        for h in new_headers:
            headers[h] = new_headers[h]

        resp = self._s.post(url, data=data, headers=headers)
        return resp

    # def _batch(self, batches, new_headers=[]):
    #     """Performs a batch request. 'batches' param is a list of tuples like
    #     (endpoint, params) where params is a params dict"""
    #
    #     batch_objects = []
    #     for endpoint, params in batches:
    #         url = endpoint
    #         urlencoded_params = urllib.urlencode(params)
    #         url_with_params = url + "?" + urlencoded_params
    #         batch_objects.append({'url': url_with_params})
    #
    #     endpoint = '/batch2'
    #     data = {
    #         'data': json.dumps({'batch': batch_objects})
    #     }
    #
    #     resp = self._post(endpoint, data, new_headers)
    #     print(resp.text)
    #     return resp 
Example 41
Project: AirPCrawler   Author: penetest   File: Spider_ChuanH_demo.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h,i_orgcity, i_dstCity):
    s = requests.Session()
    data = '{"AirlineType":"Single","IsFixedCabin":false,"RouteList":[{"RouteIndex":1,"RouteName":"单    程","OrgCity":"'+from_h+'","DesCity":"'+to_h+'","OrgCityName":"'+i_orgcity+'","DesCityName":"'+i_dstCity+'","FlightDate":"'+date_h+'"}],"AVType":0}'
    #data = urllib.urlencode(data)
    data = urllib.quote(data)
    data = 'http://www.scal.com.cn/Web/ETicket/AirlineList?AirlineParamJSON='+data
    try:
        resp = s.post(data,timeout=15)
        url = re.search(r'arrPageValue.AirlineParamJSON = (.*?);',resp.text)
        #print url.group(1)
        data = json.loads( url.group(1))
        #print(data)
        data1 = re.search(r'arrPageValue.AirlineParamJSON = .*\[(.*?)\].*;',resp.text).group(1).replace("}","")
        #data1 = str(data['RouteList'][0]).replace("}","")
        data11 = data['AirlineType']
        data1 += ',\"AirlineType\":\"'+data11+'\"'
        data11 = data['AVType']
        data1 += ',\"AVType\":'+str(data11)
        data1 += ',\"CardFlag\":null'
        data11 = data['Flag']
        data1 += ',\"Flag\":null'
        data11 = data['BuyerType']
        data1 += ',\"BuyerType\":'+str(data11)
        data11 = data['IsFixedCabin']
        data1 += ',\"IsFixedCabin\":'+str(data11).lower()
        data11 = data['PassKey']
        data1 += ',\"PassKey\":\"'+data11+'\"}'
        data1 = json.loads(data1)
        resp1 = s.post('http://www.scal.com.cn/Web/ETicket/GetSingleChina',json=data1)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 42
Project: AirPCrawler   Author: penetest   File: Spider_ChuanH_demo_win.py    MIT License 5 votes vote down vote up
def data_Crawling(from_h,to_h,date_h,i_orgcity, i_dstCity):
    s = requests.Session()
    data = '{"AirlineType":"Single","IsFixedCabin":false,"RouteList":[{"RouteIndex":1,"RouteName":"单    程","OrgCity":"'+from_h+'","DesCity":"'+to_h+'","OrgCityName":"'+i_orgcity+'","DesCityName":"'+i_dstCity+'","FlightDate":"'+date_h+'"}],"AVType":0}'
    #data = urllib.urlencode(data)
    data = urllib.quote(data)
    data = 'http://www.scal.com.cn/Web/ETicket/AirlineList?AirlineParamJSON='+data
    try:
        resp = s.post(data,timeout=15)
        url = re.search(r'arrPageValue.AirlineParamJSON = (.*?);',resp.text)
        #print url.group(1)
        data = json.loads( url.group(1))
        #print(data)
        data1 = re.search(r'arrPageValue.AirlineParamJSON = .*\[(.*?)\].*;',resp.text).group(1).replace("}","")
        #data1 = str(data['RouteList'][0]).replace("}","")
        data11 = data['AirlineType']
        data1 += ',\"AirlineType\":\"'+data11+'\"'
        data11 = data['AVType']
        data1 += ',\"AVType\":'+str(data11)
        data1 += ',\"CardFlag\":null'
        data11 = data['Flag']
        data1 += ',\"Flag\":null'
        data11 = data['BuyerType']
        data1 += ',\"BuyerType\":'+str(data11)
        data11 = data['IsFixedCabin']
        data1 += ',\"IsFixedCabin\":'+str(data11).lower()
        data11 = data['PassKey']
        data1 += ',\"PassKey\":\"'+data11+'\"}'
        data1 = json.loads(data1)
        resp1 = s.post('http://www.scal.com.cn/Web/ETicket/GetSingleChina',json=data1)
        return resp1.text,resp1.status_code
    except requests.exceptions.ReadTimeout:
        return -1,-1
    except requests.exceptions.ConnectionError:
        return -2,-2
    except AttributeError:
        #print resp.text
        return -1,-1
# 解析数据 
Example 43
Project: codepost-python   Author: codepost-io   File: assignments.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def list_submissions(self, id=None, student=None, grader=None):
        """
        Returns the list of submissions associated with an assignment, which
        optionally can be filtered according to a specific submitting `student`
        or a `grader`.
        """
        _class_type = type(self)

        id = self._get_id(id=id)

        endpoint = "{}/submissions".format(self.instance_endpoint_by_id(id=id))
        endpoint_params = {}

        if student != None:
            # Filter according to a specific student (will be URL-quoted later)
            endpoint_params["student"] = student

        if grader != None:
            # Filter according to a specific grader (will be URL-quoted later)
            endpoint_params["grader"] = grader

        if len(endpoint_params) > 0:
            endpoint += "?{}".format(_urlencode(endpoint_params))

        ret = self._requestor._request(
            endpoint=endpoint,
            method="GET",
        )
        if ret.status_code == 200:
            # Returns a list of all submissions
            return list(map(
                lambda kwargs: _submissions.Submissions(**kwargs),
                ret.json))

# ============================================================================= 
Example 44
Project: pnp   Author: HazardDede   File: storage.py    MIT License 5 votes vote down vote up
def _make_raw_file_url(shared_url):
        try:
            import urlparse
            from urllib import urlencode
        except ImportError:  # For Python 3
            import urllib.parse as urlparse
            from urllib.parse import urlencode

        url_parts = list(urlparse.urlparse(shared_url))
        # Replace all query params with ?raw=1
        url_parts[4] = urlencode({'raw': '1'})
        return urlparse.urlunparse(url_parts) 
Example 45
Project: flasky   Author: RoseOu   File: request.py    MIT License 5 votes vote down vote up
def request_encode_url(self, method, url, fields=None, **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if fields:
            url += '?' + urlencode(fields)
        return self.urlopen(method, url, **urlopen_kw) 
Example 46
Project: flasky   Author: RoseOu   File: request.py    MIT License 5 votes vote down vote up
def request_encode_url(self, method, url, fields=None, **urlopen_kw):
        """
        Make a request using :meth:`urlopen` with the ``fields`` encoded in
        the url. This is useful for request methods like GET, HEAD, DELETE, etc.
        """
        if fields:
            url += '?' + urlencode(fields)
        return self.urlopen(method, url, **urlopen_kw) 
Example 47
Project: core   Author: lifemapper   File: lm_client.py    GNU General Public License v3.0 5 votes vote down vote up
def _make_request(self, req_url, method=HTTPMethod.GET, body=None,
                      headers=None, **query_parameters):
        """Submits a request to the server and returns an open file-like object

        Args:
            req_url: The base URL to submit the request to
            method: The HTTP method used to make the request
            body: The body of the request if desired
            headers: A dictionary of headers to send with the request
            query_parameters; Any additional optional parameters sent to this
                function will be wrapped as query parameters for the request
        """
        try:
            q_params = [
                (k, v) for k, v in dict(
                    query_parameters).items() if v is not None]
            url_params = urllib.urlencode(q_params)
    
            if body is None and len(
                    q_params) > 0 and method.upper() == HTTPMethod.POST:
                body = url_params
            else:
                req_url = '{}?{}'.format(req_url, url_params)
            
            if headers is None:
                headers = {}
            req = urllib2.Request(req_url, data=body, headers=headers)
            req.get_method = lambda: method.upper()
            
            return urllib2.urlopen(req)
        except Exception, e:
            print('The failed URL was: {}'.format(req_url))
            print('Error: {}'.format(str(e)))
            raise e

    # ========================
    # = Environmental Layers =
    # ========================
    # ............................ 
Example 48
Project: core   Author: lifemapper   File: gbifquery.py    GNU General Public License v3.0 5 votes vote down vote up
def _assembleUrl(self, url, queryParams):
      for k, v in queryParams.iteritems():
         queryParams[k] = unicode(v).encode('utf-8')
               
      filterString = urllib.urlencode(queryParams)
      
      if filterString:
         url += '?%s' % filterString
         
      return url
   
# ............................................... 
Example 49
Project: core   Author: lifemapper   File: apiquery.py    GNU General Public License v3.0 5 votes vote down vote up
def _assembleKeyValFilters(self, ofDict):
        for k, v in ofDict.iteritems():
            if isinstance(v, BooleanType):
                v = str(v).lower()
            ofDict[k] = unicode(v).encode('utf-8')         
        filterString = urllib.urlencode(ofDict)
        return filterString
      
    # ............................................... 
Example 50
Project: Splunk_CBER_App   Author: MHaggis   File: client.py    MIT License 5 votes vote down vote up
def attach(self, host=None, source=None, sourcetype=None):
        """Opens a stream (a writable socket) for writing events to the index.

        :param host: The host value for events written to the stream.
        :type host: ``string``
        :param source: The source value for events written to the stream.
        :type source: ``string``
        :param sourcetype: The sourcetype value for events written to the
            stream.
        :type sourcetype: ``string``

        :return: A writable socket.
        """
        args = { 'index': self.name }
        if host is not None: args['host'] = host
        if source is not None: args['source'] = source
        if sourcetype is not None: args['sourcetype'] = sourcetype
        path = UrlEncoded(PATH_RECEIVERS_STREAM + "?" + urllib.urlencode(args), skip_encode=True)

        cookie_or_auth_header = "Authorization: %s\r\n" % self.service.token

        # If we have cookie(s), use them instead of "Authorization: ..."
        if self.service.has_cookies():
            cookie_or_auth_header = "Cookie: %s\r\n" % _make_cookie_header(self.service.get_cookies().items())

        # Since we need to stream to the index connection, we have to keep
        # the connection open and use the Splunk extension headers to note
        # the input mode
        sock = self.service.connect()
        headers = ["POST %s HTTP/1.1\r\n" % self.service._abspath(path),
                   "Host: %s:%s\r\n" % (self.service.host, int(self.service.port)),
                   "Accept-Encoding: identity\r\n",
                   cookie_or_auth_header,
                   "X-Splunk-Input-Mode: Streaming\r\n",
                   "\r\n"]
        
        for h in headers:
            sock.write(h)
        return sock