Python urlparse.unquote() Examples

The following are code examples for showing how to use urlparse.unquote(). 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: ppdecode   Author: warquel   File: __init__.py    GNU General Public License v2.0 6 votes vote down vote up
def ppdecode(url, return_all=False):
    """Parse the query and extract the decoded URL."""
    try:
        version = find_version(url)
    except AttributeError:
        raise RuntimeError('This does not appear to be a valid proofpoint url: {}'.format(url))
    else:
        if version not in valid_versions:
            raise RuntimeError('Ppdecode is unprepared to handle this version of proofpoint urls: {}'.format(url))
    parsed_url = urlparse(url)
    query_components = parse_qs(parsed_url.query)
    if sys.version_info[0] < 3:
        translated_url = query_components['u'][0].translate(string.maketrans('-_', '%/'))
    else:
        translated_url = query_components['u'][0].translate(str.maketrans('-_', '%/'))
    decoded_url = unquote(translated_url)
    query_components['decoded_url'] = decoded_url
    query_components['proofpoint_version'] = version

    return query_components 
Example 2
Project: pybrowser   Author: abranjith   File: common_utils.py    MIT License 6 votes vote down vote up
def get_unique_filename_from_url(url, ext=None, length_limit=100):
    if not url:
        return
    pieces = urlparse(url)
    parts = []
    if pieces.netloc:
        host = [n for n in pieces.netloc.split(".") 
                if n and n.strip() and (not n.lower().startswith("ww"))]
        parts.append(host[0])
    if pieces.path:
        paths = "_".join([p for p in pieces.path.split("/") if p and p.strip()])
        parts.append(paths)
    filename = "_".join(parts) + "_" + uuid1_as_str()
    filename = unquote(filename)
    filename = "_".join(filename.split())
    #limit filename
    filename = filename[:length_limit]
    if ext:
        return f"{filename}.{ext}"
    else:
        return filename 
Example 3
Project: flickr_downloader   Author: Denisolt   File: auth.py    GNU General Public License v3.0 6 votes vote down vote up
def do_request(self, url, params=None, timeout=None):
        """Performs the HTTP request, signed with OAuth.

        :param timeout: optional request timeout, in seconds.
        :type timeout: float

        @return: the response content
        """

        req = self.session.post(url,
                                data=params,
                                auth=self.oauth,
                                timeout=timeout or self.default_timeout)

        # check the response headers / status code.
        if req.status_code != 200:
            self.log.error('do_request: Status code %i received, content:', req.status_code)

            for part in req.text.split('&'):
                self.log.error('    %s', urllib_parse.unquote(part))

            raise exceptions.FlickrError('do_request: Status code %s received' % req.status_code)

        return req.content 
Example 4
Project: Shnu-Cas-Login   Author: Doreamonsky   File: Python_web.py    Apache License 2.0 6 votes vote down vote up
def do_GET(self):
        shell_command = ''

        if '?' in self.path:
            query_string = urlparse.unquote(self.path.split('?', 1)[1])
            params = urlparse.parse_qs(query_string)
            client_json = params['json'][0]
            client_data = json.loads(client_json)

            if client_data['request'] == 'courses_by_keywords':
                shell_command = 'python Shnu_course_table.py -k {0} -t json -s'.format(client_data['keywords'].encode('utf-8'))

        p = os.popen(shell_command)

        self.protocal_version = 'HTTP / 1.1'

        self.send_response(200)

        self.send_header("server", "T Engine 假装自己很牛逼")

        self.end_headers()

        self.wfile.write(p.read()) 
Example 5
Project: dubbo-python2   Author: apache   File: util.py    Apache License 2.0 6 votes vote down vote up
def parse_url(url_str):
    """
    把url字符串解析为适合于操作的对象
    :param url_str:
    :return:
    """
    url = urlparse(unquote(url_str))
    fields = dict(parse_qsl(url.query))
    result = {
        'scheme': url.scheme,
        'host': url.netloc,
        'hostname': url.hostname,
        'port': url.port,
        'path': url.path,
        'fields': fields
    }
    return result 
Example 6
Project: vk_downloader   Author: tsvstar   File: api.py    MIT License 6 votes vote down vote up
def _parse_stream_map(self, text):
        """Python's `parse_qs` can't properly decode the stream map
        containing video data so we use this instead.
        """
        videoinfo = {
            "itag": [],
            "url": [],
            "quality": [],
            "fallback_host": [],
            "s": [],
            "type": []
        }

        # Split individual videos
        videos = text.split(",")
        # Unquote the characters and split to parameters
        videos = [video.split("&") for video in videos]

        for video in videos:
            for kv in video:
                key, value = kv.split("=")
                value = value.encode('ascii')           #@tsv hack - unquote fail to parse unicode
                videoinfo.get(key, []).append(unquote(value))

        return videoinfo 
Example 7
Project: qlsync   Author: tesujimath   File: engine.py    GNU General Public License v2.0 5 votes vote down vote up
def scan_library(self):
        """Scan for quodlibet playlists."""
        # Keep Quodlibet's encoded playlist names, as trying to store
        # decoded names is troublesome.
        self.playlists = self.library.playlists()
        self.playlist_names = []
        self.playlist_index_by_name = {}
        self.playlists_on_device = [False] * len(self.playlists) # array of boolean
        i = 0
        for playlist in self.playlists:
            playlist_name = urlparse.unquote(playlist)
            self.playlist_names.append(playlist_name)
            self.playlist_index_by_name[playlist] = i
            i += 1
        self.notify_playlists_changed() 
Example 8
Project: redis-marshal   Author: mitjafelicijan   File: compat.py    MIT License 5 votes vote down vote up
def unquote_bytes_to_wsgi(bytestring):
        return unquote_to_bytes(bytestring).decode('latin-1') 
Example 9
Project: redis-marshal   Author: mitjafelicijan   File: compat.py    MIT License 5 votes vote down vote up
def unquote_bytes_to_wsgi(bytestring):
        return unquote_to_bytes(bytestring) 
Example 10
Project: bugatsinho.github.io   Author: bugatsinho   File: client.py    GNU General Public License v3.0 5 votes vote down vote up
def url2name(url):

    from os.path import basename

    url = url.split('|')[0]
    return basename(unquote(urlparse.urlsplit(url)[2])) 
Example 11
Project: poetry   Author: python-poetry   File: link.py    MIT License 5 votes vote down vote up
def filename(self):
        _, netloc, path, _, _ = urlparse.urlsplit(self.url)
        name = posixpath.basename(path.rstrip("/")) or netloc
        name = urlparse.unquote(name)
        assert name, "URL %r produced no filename" % self.url
        return name 
Example 12
Project: poetry   Author: python-poetry   File: link.py    MIT License 5 votes vote down vote up
def path(self):
        return urlparse.unquote(urlparse.urlsplit(self.url)[2]) 
Example 13
Project: a4kScrapers   Author: a4k-openproject   File: utils.py    MIT License 5 votes vote down vote up
def normalize(string):
    unescaped = unescape(string)
    unquoted = unquote(unescaped)
    return unicodedata.normalize("NFKD", unquoted).replace('\n', '') 
Example 14
Project: simple-odspy   Author: yaacov   File: sods_server.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def parse_Path(self):
		full_path = self.path.strip('/').split('?')
		self.clean_path = full_path[0]
		
		self.parameters = {}
		
		if len(full_path) == 2:
			for arg in full_path[1].split('&'):
				arg_t = arg.split('=')
				if len(arg_t) == 2:
					self.parameters[arg_t[0]] = unquote(arg_t[1])
				else:
					self.parameters[arg] = "" 
Example 15
Project: fortran-language-server   Author: hansec   File: jsonrpc.py    MIT License 5 votes vote down vote up
def path_from_uri(uri):
    # Convert file uri to path (strip html like head part)
    if not uri.startswith("file://"):
        return uri
    if os.name == "nt":
        _, path = uri.split("file:///", 1)
    else:
        _, path = uri.split("file://", 1)
    return os.path.normpath(unquote(path)) 
Example 16
Project: Maryam   Author: saeeddhqan   File: urlib.py    GNU General Public License v3.0 5 votes vote down vote up
def unquote(self):
        return urlparse.unquote(self.url) 
Example 17
Project: service.subtitles.bsplayer   Author: realgam3   File: utils.py    GNU General Public License v2.0 5 votes vote down vote up
def get_video_path(xbmc_path=''):
    xbmc_path = xbmc_path or urlparse.unquote(xbmc.Player().getPlayingFile().decode('utf-8'))
    if xbmc_path.startswith('rar://'):
        return path.dirname(xbmc_path.replace('rar://', ''))
    elif xbmc_path.startswith('stack://'):
        return xbmc_path.split(" , ")[0].replace('stack://', '')

    return xbmc_path 
Example 18
Project: pybrowser   Author: abranjith   File: common_utils.py    MIT License 5 votes vote down vote up
def guess_filename_from_url(url, has_ext=True):
    if not url:
        return
    s = url.rsplit("/")
    if s and len(s) > 0:
        filename = s[-1]
        filename = unquote(filename)
        filename = "_".join(filename.split())
        if not has_ext:
            return filename
        #return filename only if it is of the format #.#
        ws = [w for w in filename.split(".") if w.strip()]
        if len(ws) > 1:
            return filename 
Example 19
Project: ardana-ansible   Author: ArdanaCLM   File: find_zypper_iso.py    Apache License 2.0 5 votes vote down vote up
def _get_file_path(iso_uri):
    iso_uri = iso_uri.replace('iso:///?iso=', '')
    if '&' in iso_uri:
        iso, dir = iso_uri.split('&')
        dir = dir.replace('url=', '')
        dir = unquote(dir).replace('dir:', '')
        return dir + '/' + iso
    return iso_uri 
Example 20
Project: pledgeservice   Author: Lessig2016   File: compat.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def unquote_bytes_to_wsgi(bytestring):
        return unquote_to_bytes(bytestring).decode('latin-1') 
Example 21
Project: pledgeservice   Author: Lessig2016   File: compat.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def unquote_bytes_to_wsgi(bytestring):
        return unquote_to_bytes(bytestring) 
Example 22
Project: nidaba   Author: OpenPhilology   File: storage.py    GNU General Public License v2.0 5 votes vote down vote up
def get_storage_path_url(url):
    """
    Returns the file tuple for an API server URL

    Args:
        url (unicode): API server URL

    Returns:
        (unicode, unicode): file tuple
    """
    o = urlparse.urlsplit(url)[2]
    return get_storage_path(_sanitize_path(nidaba_cfg['storage_path'], urlparse.unquote(os.path.join(*o.split('/')[4:])))) 
Example 23
Project: Shnu-Cas-Login   Author: Doreamonsky   File: uwsgi_server.py    Apache License 2.0 5 votes vote down vote up
def GET(self, name):
        reload(sys)

        sys.setdefaultencoding('utf8')

        full_path = web.ctx.fullpath

        raw = urllib.unquote(full_path).encode('raw_unicode_escape')

        unicode_str = raw.decode()

        call_back_data = '{No Function}'

        adapter = Function_adapter.MyAdapter()

        if '?' in full_path:
            query_string = urlparse.unquote(unicode_str.split('?', 1)[1])

            params = urlparse.parse_qs(query_string)

            client_json = params['json'][0]

            print client_json.encode('utf-8')

            client_data = json.loads(client_json)

            print client_data['keywords'].encode('utf-8')

            call_back_data = adapter.run(client_data['request'].encode('utf-8'),
                                         client_data['keywords'].encode('utf-8'))

        del adapter

        return call_back_data 
Example 24
Project: mongo2es   Author: KentWangYQ   File: track_util.py    Apache License 2.0 5 votes vote down vote up
def url_split(url):
    obj = {}
    if url:
        r = urlparse.urlparse(url.lower())
        obj = {
            'scheme': r.scheme,
            'hostname': r.hostname,
            'path': r.path,
            'params': {}
        }
        
        if r.query:
            for q in re.split('&', r.query):
                if q:
                    #处理url中的中文
                    try:
                        q = targetDecode(q)#urlparse.unquote(q).decode('utf-8')
                    except Exception as e:
                        logger.debug(q)
                        logger.exception(e.message)
                    
                    pa = re.split('=', q)
                    if pa[0]:
                        obj['params'][pa[0].strip()] = pa[1].strip() if len(pa) > 1 else ''

    return obj 
Example 25
Project: mongo2es   Author: KentWangYQ   File: track_util.py    Apache License 2.0 5 votes vote down vote up
def targetDecode(target, encoding='utf-8', errors='strict'):
    return urlparse.unquote(
        target.encode(encoding, errors)
    ).decode(encoding, errors) 
Example 26
Project: refextract   Author: inspirehep   File: tag.py    GNU General Public License v2.0 5 votes vote down vote up
def identify_and_tag_DOI(line):
    """takes a single citation line and attempts to locate any DOI references.
       DOI references are recognised in both http (url) format and also the
       standard DOI notation (DOI: ...)
       @param line: (string) the reference line in which to search for DOI's.
       @return: the tagged line and a list of DOI strings (if any)
    """
    # Used to hold the DOI strings in the citation line
    doi_strings = []

    # Run the DOI pattern on the line, returning the re.match objects
    matched_doi = re_doi.finditer(line)
    # For each match found in the line
    for match in reversed(list(matched_doi)):
        # Store the start and end position
        start = match.start()
        end = match.end()
        # Get the actual DOI string (remove the url part of the doi string)
        doi_phrase = match.group('doi')
        if '%2f' in doi_phrase.lower():
            doi_phrase = unquote(doi_phrase)

        # Replace the entire matched doi with a tag
        line = line[0:start] + "<cds.DOI />" + line[end:]
        # Add the single DOI string to the list of DOI strings
        doi_strings.append(doi_phrase)

    doi_strings.reverse()
    return line, doi_strings 
Example 27
Project: Movie-Downloader   Author: gadilashashank   File: movie.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_openloadmovie_link():
    url = "https://duckduckgo.com/html/?q=openload+movie"
    response = requests.get(url)
    soup = bs(response.content, "lxml")
    for i in soup.find_all("a", attrs={"class": "result__a"}):
        if i.text == "Openload Movies - Watch Free HD Movies Online":
            return unquote(i['href']).replace("/l/?kh=-1&uddg=", "")


# Gets movie list from given URL 
Example 28
Project: Movie-Downloader   Author: gadilashashank   File: movie.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_curated_list(url):
    count = 1
    url_list = []
    response = requests.get(url)
    soup = bs(response.content, "lxml")
    for i in soup.find_all("div", attrs={"class": "poster"}):
        try:
            title = unquote(i.img['alt'])[:-16]
        except(AttributeError):
            title = "Unknown title"
        try:
            quality = i.find("span", attrs={"class": "quality"}).text.strip()
        except(AttributeError):
            quality = "Unknown quality"
        try:
            rating = i.find("div", attrs={"class": "rating"}).text.strip()
        except(AttributeError):
            rating = "Unknown rating"
        try:
            description = soup.find("div", attrs={"class": "texto"}).text
        except:
            description = "Description not found"
        print(str(count) + "*"*50)
        print(title + "\n")
        print(textwrap.fill(description, 50))
        print("\n" + quality + " | " + rating + " stars")
        print("*"*50)
        url_list.append(i.a['href'])
        count += 1
    return url_list


# Function to search and fire movie 
Example 29
Project: bladeBench   Author: Extent421   File: test.py    MIT License 5 votes vote down vote up
def got_data_cb(self, wid, context, x, y, data, info, time):
		paths = data.get_uris()
		for uri in paths:
			path = urlparse.unquote(urlparse.urlparse(uri).path)
			if '.bbSession' in path:
				self.loadSessionFile(path)
			else:
				self.addRow(path)

		context.finish(True, False, time) 
Example 30
Project: bladeBench   Author: Extent421   File: test.py    MIT License 5 votes vote down vote up
def got_data_ref(self, wid, context, x, y, data, info, time):
		paths = data.get_uris()
		for uri in paths:
			self.refPath = urlparse.unquote(urlparse.urlparse(uri).path[1:])
		self.refEntry.set_text( os.path.basename(self.refPath) )
		context.finish(True, False, time) 
Example 31
Project: bladeBench   Author: Extent421   File: explore.py    MIT License 5 votes vote down vote up
def got_data_cb(self, wid, context, x, y, data, info, time):
		paths = data.get_uris()
		for uri in paths:
			path = urlparse.unquote(urlparse.urlparse(uri).path)
			if '.bbSession' in path:
				self.loadSessionFile(path)
			else:
				self.addRow(path)

		context.finish(True, False, time) 
Example 32
Project: bladeBench   Author: Extent421   File: explore.py    MIT License 5 votes vote down vote up
def got_data_ref(self, wid, context, x, y, data, info, time):
		paths = data.get_uris()
		for uri in paths:
			self.refPath = urlparse.unquote(urlparse.urlparse(uri).path[1:])
		self.refEntry.set_text( os.path.basename(self.refPath) )
		context.finish(True, False, time) 
Example 33
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: runtime.py    Apache License 2.0 5 votes vote down vote up
def CgiDictFromParsedUrl(url):
  """Extract CGI variables from a parsed url into a dict.

  Returns a dict containing the following CGI variables for the provided url:
  SERVER_PORT, QUERY_STRING, SERVER_NAME and PATH_INFO.

  Args:
    url: An instance of urlparse.SplitResult.

  Returns:
    A dict containing the CGI variables derived from url.
  """
  environ = {}
  if url.port is not None:
    environ['SERVER_PORT'] = str(url.port)
  elif url.scheme == 'https':
    environ['SERVER_PORT'] = '443'
  elif url.scheme == 'http':
    environ['SERVER_PORT'] = '80'
  environ['QUERY_STRING'] = url.query
  environ['SERVER_NAME'] = url.hostname
  if url.path:
    environ['PATH_INFO'] = urlparse.unquote(url.path)
  else:
    environ['PATH_INFO'] = '/'
  return environ 
Example 34
Project: WebTrap   Author: IllusiveNetworks-Labs   File: ClonedResourceDetails.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_relative_file_path(self):
        resource_url = urlparse.urlparse(self.resource_url)
        resource_path = urlparse.unquote(resource_url.path)
        if resource_path.endswith('/'):
            resource_path += 'index.html'
        return resource_path.lstrip('/') 
Example 35
Project: -vip-   Author: wangzihan424   File: freemovie.py    MIT License 5 votes vote down vote up
def get_key(self):
        pj = webdriver.PhantomJS()
        pj.get(url=self.target)
        time.sleep(2)
        data = pj.page_source
        # req = self.s.get(url=self.target)
        # req = requests.get(url=self.target)
        # req.encoding = "utf-8"
        bf = BeautifulSoup(data, 'lxml')
        a = str(bf.find_all('iframe'))
        pattern = re.compile(r'src="(.*?)"',re.S)
        new_url = "http://www.a305.org"+pattern.findall(a)[0]
        pj.get(url=new_url)
        time.sleep(1)
        data = pj.page_source
        # ras = self.s.get(url=new_url)
        # ras.encoding = "utf-8"
        bf = BeautifulSoup(data, 'lxml')
        a = str(bf.find_all('video'))
        pattern = re.compile(r'src="(.*?)"', re.S)
        src = pattern.findall(a)[0]
        src = urlparse.unquote(src)
        if "iqiyi" in self.target:
            src = src.replace("amp;", "")
            src = "http://www.a305.org"+ src
        else:
            src = src.replace("amp;","")
        pj.close()
        return src 
Example 36
Project: ChromiumXRefs   Author: karlinjf   File: client_api.py    Apache License 2.0 5 votes vote down vote up
def GetFileSpecFromSignature(self, signature):
    KYTHE_PREFIX = "kythe://"
    assert signature.startswith(KYTHE_PREFIX)
    signature = signature[len(KYTHE_PREFIX):].split('#')[0]
    args = signature.split('?')
    assert len(args) > 1
    path = ""
    for a in args:
      if a.startswith("path="):
        path = unquote(a[5:])
    assert path != ""
    return FileSpec(name=path, package_name=args[0]) 
Example 37
Project: i2py   Author: chris-barry   File: http.py    MIT License 4 votes vote down vote up
def handle_cgi_request(methods = None):
    """
    Gets the JSON-RPC request from CGI environment and returns the
    result to STDOUT
    """

    import cgi
    import cgitb
    cgitb.enable()

    # get response-body
    request_json = sys.stdin.read()
    if request_json:
        # POST
        request_json = urlparse.unquote(request_json)
    else:
        # GET
        args = []
        kwargs = {}
        fields = cgi.FieldStorage()
        jsonrpc = fields.getfirst("jsonrpc")
        id = fields.getfirst("id")
        method = fields.getfirst("method")
        params = fields.getfirst("params")
        if params:
            params = rpcjson.loads(params)
            if isinstance(params, list):
                args = params
                kwargs = {}
            elif isinstance(params, dict):
                args = []
                kwargs = params

        # Create JSON request string
        request_dict = rpcrequest.create_request_dict(method, *args, **kwargs)
        request_dict["jsonrpc"] = jsonrpc
        request_dict["id"] = id
        request_json = rpcjson.dumps(request_dict)

    # Call
    response_json = rpclib.JsonRpc(methods = methods).call(request_json)

    # Return headers
    print "Content-Type: application/json"
    print "Cache-Control: no-cache"
    print "Pragma: no-cache"
    print

    # Return result
    print response_json 
Example 38
Project: transfer   Author: viur-framework   File: export.py    GNU General Public License v3.0 4 votes vote down vote up
def get(self, path="/", *args, **kwargs):
		self.response.headers['Content-Type'] = 'text/plain'
		# Prevent Hash-collision attacks
		assert len(self.request.arguments()) < 50
		# Fill the (surprisingly empty) kwargs dict with named request params
		tmpArgs = dict((k, self.request.get_all(k)) for k in self.request.arguments())
		for key in tmpArgs.keys()[:]:
			if len(tmpArgs[key]) == 0:
				continue
			if not key in kwargs.keys():
				if len(tmpArgs[key]) == 1:
					kwargs[key] = tmpArgs[key][0]
				else:
					kwargs[key] = tmpArgs[key]
			else:
				if isinstance(kwargs[key], list):
					kwargs[key] = kwargs[key] + tmpArgs[key]
				else:
					kwargs[key] = [kwargs[key]] + tmpArgs[key]
		del tmpArgs
		if "self" in kwargs.keys():  # self is reserved for bound methods
			raise NotImplementedError()
		path = urlparse.urlparse(path).path
		pathlist = [urlparse.unquote(x) for x in path.strip("/").split("/")]
		if len(pathlist) < 2:
			raise NotImplementedError()
		tfunc = pathlist[1]
		pathlist = pathlist[2:]
		if tfunc == "exportDb":
			self.response.write(self.exportDb(*pathlist, **kwargs))
		elif tfunc == "exportBlob":
			self.response.write(self.exportBlob(*pathlist, **kwargs))
		elif tfunc == "download":
			self.response.write(self.download(*pathlist, **kwargs))
		elif tfunc == "info":
			self.response.write(self.info(*pathlist, **kwargs))
		elif tfunc == "listCursors":
			self.response.write(self.listCursors(*pathlist, **kwargs))
		elif tfunc == "listKinds":
			self.response.write(self.listKinds(*pathlist, **kwargs))
		elif tfunc == "_ah":
			pass
		else:
			raise NotImplementedError() 
Example 39
Project: server   Author: viur-framework   File: __init__.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def selectLanguage( self, path ):
		"""
			Tries to select the best language for the current request.
		"""
		if translations is None:
			# This project doesn't use the multi-language feature, nothing to do here
			return( path )
		if conf["viur.languageMethod"] == "session":
			# We store the language inside the session, try to load it from there
			if not session.current.getLanguage():
				if "X-Appengine-Country" in self.request.headers:
					lng = self.request.headers["X-Appengine-Country"].lower()
					if lng in conf["viur.availableLanguages"]+list( conf["viur.languageAliasMap"].keys() ):
						session.current.setLanguage( lng )
						self.language = lng
					else:
						session.current.setLanguage( conf["viur.defaultLanguage"] )
			else:
				self.language = session.current.getLanguage()
		elif conf["viur.languageMethod"] == "domain":
			host = self.request.host_url.lower()
			host = host[ host.find("://")+3: ].strip(" /") #strip http(s)://
			if host.startswith("www."):
				host = host[ 4: ]
			if host in conf["viur.domainLanguageMapping"]:
				self.language = conf["viur.domainLanguageMapping"][ host ]
			else: # We have no language configured for this domain, try to read it from session
				if session.current.getLanguage():
					self.language = session.current.getLanguage()
		elif conf["viur.languageMethod"] == "url":
			tmppath = urlparse.urlparse( path ).path
			tmppath = [ urlparse.unquote( x ) for x in tmppath.lower().strip("/").split("/") ]
			if len( tmppath )>0 and tmppath[0] in conf["viur.availableLanguages"]+list( conf["viur.languageAliasMap"].keys() ):
				self.language = tmppath[0]
				return( path[ len( tmppath[0])+1: ] ) #Return the path stripped by its language segment
			else: # This URL doesnt contain an language prefix, try to read it from session
				if session.current.getLanguage():
					self.language = session.current.getLanguage()
				elif "X-Appengine-Country" in self.request.headers.keys():
					lng = self.request.headers["X-Appengine-Country"].lower()
					if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
						self.language = lng
		return( path ) 
Example 40
Project: flickr_downloader   Author: Denisolt   File: auth.py    GNU General Public License v3.0 4 votes vote down vote up
def do_upload(self, filename, url, params=None, fileobj=None, timeout=None):
        """Performs a file upload to the given URL with the given parameters, signed with OAuth.

        :param timeout: optional request timeout, in seconds.
        :type timeout: float

        @return: the response content
        """

        # work-around to allow non-ascii characters in file name
        # Flickr doesn't store the name but does use it as a default title
        if 'title' not in params:
            params['title'] = os.path.basename(filename).encode('utf8')

        # work-around for Flickr expecting 'photo' to be excluded
        # from the oauth signature:
        #   1. create a dummy request without 'photo'
        #   2. create real request and use auth headers from the dummy one
        dummy_req = requests.Request('POST', url, data=params,
                                     auth=self.oauth)

        prepared = dummy_req.prepare()
        headers = prepared.headers
        self.log.debug('do_upload: prepared headers = %s', headers)

        if not fileobj:
            fileobj = open(filename, 'rb')
        params['photo'] = ('dummy name', fileobj)
        m = MultipartEncoder(fields=params)
        auth = {'Authorization': headers.get('Authorization'),
                'Content-Type': m.content_type}
        self.log.debug('POST %s', auth)
        req = self.session.post(url, data=m, headers=auth, timeout=timeout or self.default_timeout)

        # check the response headers / status code.
        if req.status_code != 200:
            self.log.error('do_upload: Status code %i received, content:', req.status_code)

            for part in req.text.split('&'):
                self.log.error('    %s', urllib_parse.unquote(part))

            raise exceptions.FlickrError('do_upload: Status code %s received' % req.status_code)

        return req.content 
Example 41
Project: kasir   Author: iColdPlayer   File: redis.py    MIT License 4 votes vote down vote up
def _params_from_url(self, url, defaults):
        scheme, host, port, _, password, path, query = _parse_url(url)
        connparams = dict(
            defaults, **dictfilter({
                'host': host, 'port': port, 'password': password,
                'db': query.pop('virtual_host', None)})
        )

        if scheme == 'socket':
            # use 'path' as path to the socket… in this case
            # the database number should be given in 'query'
            connparams.update({
                'connection_class': self.redis.UnixDomainSocketConnection,
                'path': '/' + path,
            })
            # host+port are invalid options when using this connection type.
            connparams.pop('host', None)
            connparams.pop('port', None)
            connparams.pop('socket_connect_timeout')
        else:
            connparams['db'] = path

        ssl_param_keys = ['ssl_ca_certs', 'ssl_certfile', 'ssl_keyfile',
                          'ssl_cert_reqs']

        if scheme == 'redis':
            # If connparams or query string contain ssl params, raise error
            if (any(key in connparams for key in ssl_param_keys) or
                    any(key in query for key in ssl_param_keys)):
                raise ValueError(E_REDIS_SSL_PARAMS_AND_SCHEME_MISMATCH)

        if scheme == 'rediss':
            connparams['connection_class'] = redis.SSLConnection
            # The following parameters, if present in the URL, are encoded. We
            # must add the decoded values to connparams.
            for ssl_setting in ssl_param_keys:
                ssl_val = query.pop(ssl_setting, None)
                if ssl_val:
                    connparams[ssl_setting] = unquote(ssl_val)

        # db may be string and start with / like in kombu.
        db = connparams.get('db') or 0
        db = db.strip('/') if isinstance(db, string_t) else db
        connparams['db'] = int(db)

        for key, value in query.items():
            if key in redis.connection.URL_QUERY_ARGUMENT_PARSERS:
                query[key] = redis.connection.URL_QUERY_ARGUMENT_PARSERS[key](
                    value
                )

        # Query parameters override other parameters
        connparams.update(query)
        return connparams