Python urllib.splitquery() Examples

The following are 7 code examples of urllib.splitquery(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib , or try the search function .
Example #1
Source File: test_urllib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_splitquery(self):
        # Normal cases are exercised by other tests; ensure that we also
        # catch cases with no port specified (testcase ensuring coverage)
        splitquery = urllib.splitquery
        self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
                         ('http://python.org/fake', 'foo=bar'))
        self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
                         ('http://python.org/fake?foo=bar', ''))
        self.assertEqual(splitquery('http://python.org/fake'),
                         ('http://python.org/fake', None))
        self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar')) 
Example #2
Source File: file-server.py    From http-file-server with MIT License 5 votes vote down vote up
def do_GET(self):
        query = urllib.splitquery(self.path)
        path = urllib.unquote_plus(query[0]).decode("utf-8", "ignore")
        queryParams = {}

        if "?" in self.path:
            if query[1]:
                queryParams = transDicts(query[1])

        fn = "%s%s" % (g_filepath, path)
        fn = urllib.unquote_plus(fn).decode("utf-8", "ignore")
        fn = fn.replace("/",os.sep)

        content = ""
        self.send_response(200)
        self.send_header("content-type","application/json")
        if os.path.isfile(fn):
            f = open(fn, "rb")
            content = f.read()
            f.close()
            contenttype,_ = mimetypes.guess_type(fn)
            if contenttype:
                self.send_header("content-type",contenttype)
        elif os.path.isdir(fn):
            filelist = []
            for filename in os.listdir(fn):
                if filename[0] != ".":
                    filepath = "%s%s%s" % (fn, os.sep, filename)
                    if os.path.isdir(filepath):
                        filename += os.sep
                    mtime = os.path.getmtime(filepath)
                    filelist.append({"filename":filename,"mtime":mtime})
            content = json.dumps(filelist)
        else:
            print(g_filepath, path, fn)
            content = "<h1>404<h1>"
            self.send_header("content-type","text/html")

        self.end_headers()
        self.wfile.write(content) 
Example #3
Source File: file-server.py    From http-file-server with MIT License 5 votes vote down vote up
def do_POST(self):
        query = urllib.splitquery(self.path)
        path = query[0]
        queryParams = {}

        if "?" in self.path:
            if query[1]:
                queryParams = transDicts(query[1])

        resultdict = {"result":0, "msg":"OK"}
        if path=="/upload":
            if queryParams.has_key("name"):
                filesize = int(self.headers["content-length"])
                filecontent = self.rfile.read(filesize)
                fn = queryParams["name"]
                resultdict["filename"] = fn
                fn = "%s%s" % (g_filepath, fn)
                dirname = os.path.dirname(fn)
                if not os.path.exists(dirname):
                    os.makedirs(dirname)
                if os.path.isdir(fn):
                    resultdict.result = 1
                    resultdict.msg = "File name is directory."
                else:
                    f = open(fn,"wb")
                    f.write(filecontent)
                    f.close()
            else:
                resultdict.result = 2
                resultdict.msg = "Need file name."
        else:
            resultdict.result = 3
            resultdict.msg = "No this API."

        content = json.dumps(resultdict)
        self.send_response(200)
        self.send_header("content-type","application/json")
        self.end_headers()
        self.wfile.write(content) 
Example #4
Source File: test_urllib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_splitquery(self):
        # Normal cases are exercised by other tests; ensure that we also
        # catch cases with no port specified (testcase ensuring coverage)
        splitquery = urllib.splitquery
        self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
                         ('http://python.org/fake', 'foo=bar'))
        self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
                         ('http://python.org/fake?foo=bar', ''))
        self.assertEqual(splitquery('http://python.org/fake'),
                         ('http://python.org/fake', None))
        self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar')) 
Example #5
Source File: DLProcessor.py    From iqiyi-parser with MIT License 5 votes vote down vote up
def makeReqHeaders(self):

        range_format = self.url.range_format
        Range = (self.progress.begin + self.progress.go_inc, self.progress.end)

        req_path = self.target.path

        req_headers = dict(self.url.headers.items())

        if range_format[0] == '&':
            path, query = splitquery(self.target.path)
            query_dict = extract_query(query)
            range_format = range_format % Range
            for i in range_format[1:].split('&'):
                param_key, param_value = splitvalue(i)
                query_dict[param_key] = param_value

            new_query = urlencode(query_dict)
            req_path = '%s?%s' % (path, new_query)

        else:

            range_field = range_format % Range
            key_value = [i.strip() for i in range_field.split(':')]

            key = key_value[0]
            value = key_value[1]

            add_headers = {
                key: value,
                'Accept-Ranges': 'bytes'
            }

            req_headers.update(add_headers)

        return req_path, req_headers 
Example #6
Source File: test_urllib.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_splitquery(self):
        # Normal cases are exercised by other tests; ensure that we also
        # catch cases with no port specified (testcase ensuring coverage)
        splitquery = urllib.splitquery
        self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
                         ('http://python.org/fake', 'foo=bar'))
        self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
                         ('http://python.org/fake?foo=bar', ''))
        self.assertEqual(splitquery('http://python.org/fake'),
                         ('http://python.org/fake', None))
        self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar')) 
Example #7
Source File: http.py    From cutout with MIT License 4 votes vote down vote up
def process(self, type):
		#是否注册了处理程序
		query = urllib.parse.splitquery(self.path)
		path = query[0]
		if not path in self.respondFunc:
			return self.send_error(404, 'File Not Found: %s' % path)
		#获取get和post数据
		get, post = self.get_post_data(query)
		content = self.respondFunc[path](get, post)
		try:
			self.send_response(200)
			self.send_header('Content-type','text/html')
			self.end_headers()
			if content:
				self.wfile.write(content.encode())
			else:
				self.wfile.write(b'')
			#f.close()
		except IOError:
			self.send_error(500, 'System error : %s' % path)


		'''
		#print(str(self))
		#return
		content = ""
		if type==1:#post方法,接收post参数
			datas = self.rfile.read(int(self.headers['content-length']))
			datas = urllib.unquote(datas).decode("utf-8", 'ignore')#指定编码方式
			datas = transDicts(datas)#将参数转换为字典
			if datas.has_key('data'):
				content = "data:"+datas['data']+"\r\n"
		if '?' in self.path:
			query = urllib.splitquery(self.path)
			action = query[0]
			if query[1]:#接收get参数
				queryParams = {}
				for qp in query[1].split('&'):
					kv = qp.split('=')
					queryParams[kv[0]] = urllib.unquote(kv[1]).decode("utf-8", 'ignore')
					content+= kv[0]+':'+queryParams[kv[0]]+"\r\n"
		#指定返回编码
		enc="UTF-8"
		content = content.encode(enc)
		f = io.BytesIO()
		f.write(content)
		f.seek(0)
		self.send_response(200)
		self.send_header("Content-type", "text/html; charset=%s" % enc)
		self.send_header("Content-Length", str(len(content)))
		self.end_headers()
		shutil.copyfileobj(f,self.wfile)
		'''


##注册处理程序