當前位置: 首頁>>代碼示例>>Python>>正文


Python urllib.splitquery方法代碼示例

本文整理匯總了Python中urllib.splitquery方法的典型用法代碼示例。如果您正苦於以下問題:Python urllib.splitquery方法的具體用法?Python urllib.splitquery怎麽用?Python urllib.splitquery使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在urllib的用法示例。


在下文中一共展示了urllib.splitquery方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: do_GET

# 需要導入模塊: import urllib [as 別名]
# 或者: from urllib import splitquery [as 別名]
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) 
開發者ID:hanxi,項目名稱:http-file-server,代碼行數:42,代碼來源:file-server.py

示例2: test_splitquery

# 需要導入模塊: import urllib [as 別名]
# 或者: from urllib import splitquery [as 別名]
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')) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:13,代碼來源:test_urllib.py

示例3: do_POST

# 需要導入模塊: import urllib [as 別名]
# 或者: from urllib import splitquery [as 別名]
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) 
開發者ID:hanxi,項目名稱:http-file-server,代碼行數:41,代碼來源:file-server.py

示例4: makeReqHeaders

# 需要導入模塊: import urllib [as 別名]
# 或者: from urllib import splitquery [as 別名]
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 
開發者ID:ZSAIm,項目名稱:iqiyi-parser,代碼行數:38,代碼來源:DLProcessor.py

示例5: process

# 需要導入模塊: import urllib [as 別名]
# 或者: from urllib import splitquery [as 別名]
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)
		'''


##注冊處理程序 
開發者ID:jojoin,項目名稱:cutout,代碼行數:58,代碼來源:http.py


注:本文中的urllib.splitquery方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。