本文整理汇总了Python中twisted.web.http.parse_qs函数的典型用法代码示例。如果您正苦于以下问题:Python parse_qs函数的具体用法?Python parse_qs怎么用?Python parse_qs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_qs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: requestReceived
def requestReceived(self, command, path, version):
"""
Called by channel when all data has been received.
Overridden because Twisted merges GET and POST into one thing by default.
"""
self.content.seek(0,0)
self.get = {}
self.post = {}
self.method, self.uri = command, path
self.clientproto = version
x = self.uri.split(b'?', 1)
print self.method
# URI and GET args assignment
if len(x) == 1:
self.path = self.uri
else:
self.path, argstring = x
self.get = parse_qs(argstring, 1)
# cache the client and server information, we'll need this later to be
# serialized and sent with the request so CGIs will work remotely
self.client = self.channel.transport.getPeer()
self.host = self.channel.transport.getHost()
# Argument processing
ctype = self.requestHeaders.getRawHeaders(b'content-type')
if ctype is not None:
ctype = ctype[0]
# Process POST data if present
if self.method == b"POST" and ctype:
mfd = b'multipart/form-data'
key, pdict = _parseHeader(ctype)
if key == b'application/x-www-form-urlencoded':
self.post.update(parse_qs(self.content.read(), 1))
elif key == mfd:
try:
cgiArgs = cgi.parse_multipart(self.content, pdict)
if _PY3:
# parse_multipart on Python 3 decodes the header bytes
# as iso-8859-1 and returns a str key -- we want bytes
# so encode it back
self.post.update({x.encode('iso-8859-1'): y
for x, y in cgiArgs.items()})
else:
self.post.update(cgiArgs)
except:
# It was a bad request.
_respondToBadRequestAndDisconnect(self.channel.transport)
return
self.content.seek(0, 0)
# Continue with rest of request handling
self.process()
示例2: testParseqs
def testParseqs(self):
self.failUnlessEqual(cgi.parse_qs("a=b&d=c;+=f"),
http.parse_qs("a=b&d=c;+=f"))
self.failUnlessRaises(ValueError, http.parse_qs, "blah",
strict_parsing = 1)
self.failUnlessEqual(cgi.parse_qs("a=&b=c", keep_blank_values = 1),
http.parse_qs("a=&b=c", keep_blank_values = 1))
self.failUnlessEqual(cgi.parse_qs("a=&b=c"),
http.parse_qs("a=&b=c"))
示例3: requestReceived
def requestReceived(self, command, path, version):
from twisted.web.http import parse_qs
if self.do_log:
print '%f Request Received' % time.time()
self.content.seek(0,0)
self.args = {}
self.stack = []
self.method, self.uri = command, path
self.clientproto = version
x = self.uri.split(b'?', 1)
if len(x) == 1:
self.path = self.uri
else:
self.path, argstring = x
self.args = parse_qs(argstring, 1)
# cache the client and server information, we'll need this later to be
# serialized and sent with the request so CGIs will work remotely
self.client = self.channel.transport.getPeer()
self.host = self.channel.transport.getHost()
# Argument processing
args = self.args
ctype = self.requestHeaders.getRawHeaders(b'content-type')
if ctype is not None:
ctype = ctype[0]
if self.method == b"POST" and ctype:
mfd = b'multipart/form-data'
key, pdict = cgi.parse_header(ctype)
if key == b'application/x-www-form-urlencoded':
args.update(parse_qs(self.content.read(), 1))
elif key == mfd:
try:
self.content.seek(0,0)
args.update(self.parse_multipart(self.content, pdict))
#args.update(cgi.parse_multipart(self.content, pdict))
except KeyError as e:
if e.args[0] == b'content-disposition':
# Parse_multipart can't cope with missing
# content-dispostion headers in multipart/form-data
# parts, so we catch the exception and tell the client
# it was a bad request.
self.channel.transport.write(
b"HTTP/1.1 400 Bad Request\r\n\r\n")
self.channel.transport.loseConnection()
return
raise
self.content.seek(0, 0)
self.process()
示例4: _parse_request_args
def _parse_request_args(self, route):
"""
Parses JSON data and request form if present
"""
data = self.request.content.read()
data_json = {}
if self.request.method in ['POST', 'PUT']:
ct = self.request.requestHeaders.getRawHeaders('content-type')
if 'application/json' in ct:
try:
data_json = json.loads(data)
except ValueError:
data_json = {}
request_args = self.request.args
request_headers = self.request.requestHeaders.getRawHeaders(
'content-type'
)
if self.request.method == 'PUT':
if 'application/x-www-form-urlencoded' in request_headers:
request_args = parse_qs(data, 1)
if len(request_args) > 0:
for key, value in request_args.iteritems():
if key not in route.callback_args:
route.callback_args.update({key: value[0]})
elif data_json:
for key, value in data_json.iteritems():
if key not in route.callback_args:
route.callback_args.update({key: value})
示例5: __init__
def __init__(self, server, priv_request):
log.Logger.__init__(self, server)
log.LogProxy.__init__(self, server)
self._server = server
self._ref = priv_request
self._secured = server.is_secured
content_type = self.get_header("content-type")
mime_type, encoding = http.parse_content_type(content_type)
language = self.get_header("content-language") or http.DEFAULT_LANGUAGE
location = http.path2tuple(self._ref.path)
accept = self.get_header("accept")
accepted_mime_types = http.parse_accepted_types(accept)
accept_tree = http.build_mime_tree(accepted_mime_types)
accept_charset = self.get_header("accept-charset")
accepted_encodings = http.parse_accepted_charsets(accept_charset)
accept_languages = self.get_header("accept-languages")
accepted_languages = http.parse_accepted_languages(accept_languages)
method = http.Methods[self._ref.method]
protocol = _protocol_lookup[self._ref.clientproto]
self._mime_type = mime_type
self._encoding = encoding
self._language = language
self._location = location
self._accepted_mime_types = accepted_mime_types
self._accept_tree = accept_tree
self._accepted_encodings = accepted_encodings
self._accepted_languages = accepted_languages
self._method = method
self._protocol = protocol
self._credentials = None
self._context = {} # Black box
self._reading = False
self._objects = []
# Look for URI arguments only, the POST is content, not arguments
uri_parts = self._ref.uri.split("?", 1)
if len(uri_parts) > 1:
arguments = twhttp.parse_qs(uri_parts[1], 1)
else:
arguments = {}
# Look for domain information
domain = self.get_header("host")
if not domain:
domain = "%s:%s" % (self._ref.host.host, self._ref.host.port)
domain = self._decode(domain)
content_length = self.get_header("content-length")
length = content_length and int(content_length)
# To prevent content being consumed
# when it's application/x-www-form-urlencoded
self._ref.content.seek(0, 0)
self._arguments = arguments
self._domain = domain
self._length = length
示例6: render
def render(self, request):
self.logger.info("render(%r)" % request)
self.logger.debug("request.method: %s", request.method)
self.logger.debug("request.URLPath(): %s", request.URLPath())
self.logger.debug("request.uri: %s", request.uri)
self.logger.debug("request.path: %s", request.path)
self._logRequestHeaders(request)
self.logger.debug("Client IP: %s", request.getClientIP())
self.logger.debug("request.getHost(): %s", request.getHost())
self.logger.debug("request.getRequestHostname(): %s", request.getRequestHostname())
authenticated, response = self.checkAuth(request)
if not authenticated:
return response
from twisted.web import http
if request.method == "PUT" and len(request.args) == 0:
request.args = http.parse_qs(request.content.read(), 1)
if request.getHeader("Origin"):
result = self.handleCors(request)
if result:
return result
response = None
try:
response = resource.Resource.render(self, request)
except:
self.logger.exception("Exception during resource rendering")
raise
self._logResponseHeaders(request)
return response
示例7: __init__
def __init__(self, api_mode="test", api_version="0.1", api_name="TestAPI", uri="",
method="GET", user="", password=""):
self.headers = {}
self.args = {}
self.cookies = []
self.received_cookies = {}
self.client_ip = "4.3.2.1"
self.content = StringIO()
self._finishedDeferreds = []
self.api_mode = api_mode
self.api_version = api_version
self.api_name = api_name
self.uri = uri
self.user = user
self.password = password
self.method = method
self.ignore_auth = True
self.ignore_secure_cookies = True
x = self.uri.split(b'?', 1)
if len(x) == 1:
self.path = self.uri
else:
self.path, argstring = x
self.args = parse_qs(argstring, 1)
示例8: render_PUT
def render_PUT(self, request):
parameters = http.parse_qs(request.content.read(), 1)
if 'name' not in parameters or not parameters['name'] or not parameters['name'][0]:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "channel name cannot be empty"})
if 'description' not in parameters or not parameters['description']:
description = u''
else:
description = unquote(parameters['description'][0]).decode('utf-8')
my_key = self.session.trustchain_keypair
my_channel_pk = my_key.pub().key_to_bin()
# Do not allow to add a channel twice
if self.session.lm.mds.get_my_channel():
request.setResponseCode(http.CONFLICT)
return json.dumps({"error": "channel already exists"})
title = unquote(parameters['name'][0]).decode('utf-8')
self.session.lm.mds.ChannelMetadata.create_channel(title, description)
return json.dumps({
"added": hexlify(str(my_channel_pk)),
})
示例9: __addRequestArguments
def __addRequestArguments(self,request,allargs):
"""Parses the request form arguments OR JSON document root elements to build the list of arguments to a method"""
# handler for weird Twisted logic where PUT does not get form params
# see: http://twistedmatrix.com/pipermail/twisted-web/2007-March/003338.html
requestargs = request.args
if request.method == Http.PUT and HttpHeader.CONTENT_TYPE in request.received_headers.keys() \
and request.received_headers[HttpHeader.CONTENT_TYPE] == MediaType.APPLICATION_FORM_URLENCODED:
# request.data is populated in __parseRequestData
requestargs = parse_qs(request.data, 1)
#merge form args
if len(requestargs.keys()) > 0:
for arg in requestargs.keys():
# maintain first instance of an argument always
safeDictUpdate(allargs,arg,requestargs[arg][0])
elif hasattr(request,'json'):
# if YAML parse root elements instead of form elements
for key in request.json.keys():
safeDictUpdate(allargs, key, request.json[key])
elif hasattr(request,'yaml'):
# if YAML parse root elements instead of form elements
for key in request.yaml.keys():
safeDictUpdate(allargs, key, request.yaml[key])
elif hasattr(request,'xml'):
# if XML, parse attributes first, then root nodes
for key in request.xml.attrib:
safeDictUpdate(allargs, key, request.xml.attrib[key])
for el in request.xml.findall("*"):
safeDictUpdate(allargs, el.tag,el.text)
示例10: render_PUT
def render_PUT(self, request):
#take parameters. In this case, the content for update the file
args = http.parse_qs(request.content.read(), 1)
if not args:
return resource.ErrorPage(400, "INCORRECT PARAMETERS", "Supervise parameters, you not put anything to update ").render(request)
try:
_, file_resource, file_id, _ = split_path(request.path, 4, 4, True)
except:
return resource.ErrorPage(400, "INCORRECT PARAMETERS", "Supervise parameters, the correct form is api/file/file_id/data ").render(request)
# We look up the name of file, and full path, to update it.
message = api_library.get_metadata(1234, file_id)
if not message:
return resource.ErrorPage(404, "NOT FOUND", "File or folder not found at the specified path:" + request.path).render(request)
message = json.loads(message)
# Create new version to save old content version
#TODO: Define mimetype, size and chunk
message_new_version = api_library.update_data(1234, message["id"], message["parent"], None, None, None)
if not message_new_version:
return resource.ErrorPage(404, "NOT FOUND", "Some problem to create a new version of file").render(request)
# TODO: Using name and full path update the file into DB, using new version.
request.setHeader("content-type", "application/json")
request.finish()
return server.NOT_DONE_YET
示例11: render_PUT
def render_PUT(self, request):
"""
.. http:put:: /market/asks
A request to this endpoint will create a new ask order.
**Example request**:
.. sourcecode:: none
curl -X PUT http://localhost:8085/market/asks --data
"first_asset_amount=10&second_asset_amount=10&first_asset_type=BTC&second_asset_type=MB"
**Example response**:
.. sourcecode:: javascript
{
"timestamp": 1547587907.887339,
"order_number": 12,
"assets": {
"second": {
"amount": 1000,
"type": "MB"
},
"first": {
"amount": 100000,
"type": "BTC"
}
},
"timeout": 3600,
"trader_id": "9695c9e15201d08586e4230f4a8524799ebcb2d7"
}
"""
parameters = http.parse_qs(request.content.read(), 1)
if not has_param(parameters, 'first_asset_amount') or not has_param(parameters, 'second_asset_amount'):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "asset amount parameter missing"})
if not has_param(parameters, 'first_asset_type') or not has_param(parameters, 'second_asset_type'):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "asset type parameter missing"})
def on_ask_created(ask):
if not request.finished:
request.write(json.dumps({
'assets': ask.assets.to_dictionary(),
'timestamp': float(ask.timestamp),
'trader_id': str(ask.order_id.trader_id),
'order_number': int(ask.order_id.order_number),
'timeout': int(ask.timeout)
}))
request.finish()
self.get_market_community().create_ask(*BaseAsksBidsEndpoint.create_ask_bid_from_params(parameters))\
.addCallback(on_ask_created)
return NOT_DONE_YET
示例12: render_DELETE
def render_DELETE(self, request):
"""
.. http:delete:: /downloads/(string: infohash)
A DELETE request to this endpoint removes a specific download from Tribler. You can specify whether you only
want to remove the download or the download and the downloaded data using the remove_data parameter.
**Example request**:
.. sourcecode:: none
curl -X DELETE http://localhost:8085/download/4344503b7e797ebf31582327a5baae35b11bda01
--data "remove_data=1"
**Example response**:
.. sourcecode:: javascript
{"removed": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
"""
parameters = http.parse_qs(request.content.read(), 1)
if 'remove_data' not in parameters or len(parameters['remove_data']) == 0:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "remove_data parameter missing"})
download = self.session.get_download(self.infohash)
if not download:
return DownloadSpecificEndpoint.return_404(request)
remove_data = parameters['remove_data'][0] == "1"
def _on_torrent_removed(_):
"""
Success callback
"""
request.write(json.dumps({"removed": True,
"infohash": hexlify(download.get_def().get_infohash())}))
request.finish()
def _on_remove_failure(failure):
"""
Error callback
:param failure: from remove_download
"""
self._logger.exception(failure)
request.write(return_handled_exception(request, failure.value))
# If the above request.write failed, the request will have already been finished
if not request.finished:
request.finish()
deferred = self.session.remove_download(download, remove_content=remove_data)
deferred.addCallback(_on_torrent_removed)
deferred.addErrback(_on_remove_failure)
return NOT_DONE_YET
示例13: render_PATCH
def render_PATCH(self, request):
"""
.. http:put:: /mychannel/torrents/(string: torrent infohash)
Edit tags, status or title of a torrent entry in a your channel.
The properties to edit should be provided in the PATCH data as a URL-encoded dict.
On success, it returns a new status for the torrent.
**Example request**:
.. sourcecode:: none
curl -X PUT http://localhost:8085/mychannel/torrents/97d2..151
--data "tags=Video"
**Example response**:
.. sourcecode:: javascript
{
"success": 1,
"new_status": 6,
"dirty": 1
}
:statuscode 404: if your channel or the infohash does not exist.
:statuscode 500: if the passed arguments data is wrong.
"""
parameters_raw = http.parse_qs(request.content.read(), 1)
parameters = {}
# FIXME: make all endpoints Unicode-compatible in a unified way
for param, val in viewitems(parameters_raw):
parameters.update({param: [item.decode('utf-8') for item in val]})
if 'status' not in parameters and 'tags' not in parameters and 'title' not in parameters:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "attribute to change is missing"})
if 'status' in parameters and ('tags' in parameters or 'title' in parameters):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "cannot set status manually when changing other parameters"})
my_channel = self.session.lm.mds.ChannelMetadata.get_my_channel()
if not my_channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": "your channel has not been created"})
torrent = my_channel.get_torrent(self.infohash)
if not torrent:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": "torrent with the specified infohash could not be found"})
torrent.update_properties(dict([(attribute, parameters[attribute][0]) for attribute in
['status', 'tags', 'title'] if attribute in parameters]))
return json.dumps({"success": True, "new_status": torrent.status, "dirty": my_channel.dirty})
示例14: render_POST
def render_POST(self, request):
my_channel = tribler_utils.tribler_data.get_my_channel()
if my_channel is None:
return MyChannelBaseEndpoint.return_404(request)
parameters = http.parse_qs(request.content.read(), 1)
my_channel.name = parameters['name'][0]
my_channel.description = parameters['description'][0]
return json.dumps({"edited": my_channel.id})
示例15: render_PUT
def render_PUT(self, request):
parameters = http.parse_qs(request.content.read(), 1)
print parameters
channel_name = parameters['name'][0]
channel_description = parameters['description'][0]
my_channel = Channel(len(tribler_utils.tribler_data.channels) - 1,
name=channel_name, description=channel_description)
tribler_utils.tribler_data.channels.append(my_channel)
tribler_utils.tribler_data.my_channel = my_channel.id
return json.dumps({"added": my_channel.id})