本文整理匯總了Python中cgi.parse_multipart方法的典型用法代碼示例。如果您正苦於以下問題:Python cgi.parse_multipart方法的具體用法?Python cgi.parse_multipart怎麽用?Python cgi.parse_multipart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cgi
的用法示例。
在下文中一共展示了cgi.parse_multipart方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self): # NOQA
content_type, pdict = cgi.parse_header(self.headers.getheader("content-type"))
fields = cgi.parse_multipart(self.rfile, pdict)
if self.path.startswith("/anytask/submit"):
if "_failed_" in fields["file"][0]:
reply = {
'error': {
'message': "Submit error in fake server!"
}
}
else:
reply = {
'result': {
'value': "1",
}
}
self.send_response(200)
self.end_headers()
json.dump(reply, self.wfile)
return
self.send_response(501)
self.end_headers()
示例2: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
"""
Handle POST.
"""
if self.path != "/interact":
return self.respond({"status": 500})
ctype, pdict = cgi.parse_header(self.headers["content-type"])
pdict["boundary"] = bytes(pdict["boundary"], "utf-8")
postvars = cgi.parse_multipart(self.rfile, pdict)
if postvars["image"][0].decode() != "":
SHARED["dialog_history"] = []
SHARED["image_feats"] = None
model_response = self.interactive_running(postvars)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
json_str = json.dumps(model_response)
self.wfile.write(bytes(json_str, "utf-8"))
示例3: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
"""
Handle POST.
"""
if self.path != '/interact':
return self.respond({'status': 500})
ctype, pdict = cgi.parse_header(self.headers['content-type'])
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
postvars = cgi.parse_multipart(self.rfile, pdict)
model_response = self.interactive_running(postvars)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
json_str = json.dumps(model_response)
self.wfile.write(bytes(json_str, 'utf-8'))
示例4: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
"""Handle POST."""
if self.path != "/interact":
return self.respond({"status": 500})
ctype, pdict = cgi.parse_header(self.headers["content-type"])
pdict["boundary"] = bytes(pdict["boundary"], "utf-8")
postvars = cgi.parse_multipart(self.rfile, pdict)
if postvars["image"][0].decode() != "":
SHARED["dialog_history"] = []
SHARED["image_feats"] = None
model_response = self.interactive_running(postvars)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
json_str = json.dumps(model_response)
self.wfile.write(bytes(json_str, "utf-8"))
示例5: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
#content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
# post_data = self.rfile.read(content_length) # <--- Gets the data itself
ctype, pdict = cgi.parse_header(self.headers['content-type'])
print(pdict)
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
multipart_data = cgi.parse_multipart(self.rfile, pdict)
filename = uuid.uuid1()
fo = open("tmp/%s.jpg"%filename, "wb")
# print(str(multipart_data))
# print(multipart_data.get('pic')[0])
fo.write( multipart_data.get('pic')[0] )
fo.close()
result = process("tmp/%s.jpg"%filename)
#print result
self._set_headers()
self.send_header("Content-Length", str(len(json.dumps(result).encode('utf-8'))))
self.end_headers()
self.wfile.write(json.dumps(result).encode('utf-8'))
示例6: parse_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def parse_POST(self):
try:
ctype, pdict = parse_header(self.headers['content-type'])
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
elif ctype == 'application/json':
length = int(self.headers['content-length'])
postvars = json.loads(self.rfile.read(length))
else:
postvars = {}
except (KeyError, TypeError):
postvars = {}
# TODO: Standardize parsed data
return postvars
示例7: _decode_multipart
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def _decode_multipart(content: bytes, content_type: str) -> Dict[str, str]:
# a simplified version of multipart encoding that satisfies testing purposes
_, options = cgi.parse_header(content_type)
options["boundary"] = options["boundary"].encode()
options["CONTENT-LENGTH"] = len(content)
return {key: value[0].decode() for key, value in cgi.parse_multipart(io.BytesIO(content), options).items()}
示例8: get_data
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def get_data(self):
"""
Returns the JSON data converted to a dict depending of the content-type sent. Only if data format is correct,
returns the dict, otherwise, returns None
"""
if self.data is None:
if self.is_json_content_type():
try:
self.data = json.loads(self.get_content())
except ValueError:
self.data = None
elif self.is_form_urlencoded_data_content_type():
parsed_data = parse_qs(self.get_content(), keep_blank_values=True)
self.data = dict(map(
lambda t: (t[0], t[1][0] if type(t[1]) == list and len(t[1]) == 1 else t[1]), parsed_data.items()
))
elif self.is_multipart_form_data_content_type():
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if 'boundary' in pdict:
pdict['boundary'] = pdict['boundary'].encode()
content = self.get_content(decode=False)
filenames = re.findall(r'filename="(.*?)"', str(content), re.IGNORECASE | re.DOTALL | re.MULTILINE)
parsed_data = cgi.parse_multipart(BytesIO(content), pdict)
self.data = {}
for key in parsed_data:
parsed_item = parsed_data[key]
if type(parsed_item) == list:
for content in parsed_item:
try:
self.data[key] = parsed_item[0].decode('utf-8')
except UnicodeDecodeError as e:
# we assume that are files
try:
filename = filenames.pop(0)
except IndexError as e:
filename = None
self.data[key] = self.save_as_file(content, filename)
else:
self.data[key] = parsed_item
return self.data
示例9: _parseHeader
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def _parseHeader(line):
# cgi.parse_header requires a str
key, pdict = cgi.parse_header(line.decode('charmap'))
# We want the key as bytes, and cgi.parse_multipart (which consumes
# pdict) expects a dict of str keys but bytes values
key = key.encode('charmap')
pdict = {x:y.encode('charmap') for x, y in pdict.items()}
return (key, pdict)
示例10: test_parse_multipart
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def test_parse_multipart(self):
fp = BytesIO(POSTDATA.encode('latin1'))
env = {'boundary': BOUNDARY.encode('latin1'),
'CONTENT-LENGTH': '558'}
result = cgi.parse_multipart(fp, env)
expected = {'submit': [b' Add '], 'id': [b'1234'],
'file': [b'Testing 123.\n'], 'title': [b'']}
self.assertEqual(result, expected)
示例11: parse_post_vars
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def parse_post_vars(self):
ctype, pdict = cgi.parse_header(self.headers['content-type'])
if ctype == 'multipart/form-data':
postvars = cgi.parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
else:
postvars = {}
return postvars
示例12: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
length = int(self.headers.getheader('content-length'))
if ctype == 'multipart/form-data':
postvars = cgi.parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
else:
postvars = {}
if self.path == '/Forms/login_security_1.html':
auth = Auth()
if auth.http_client_auth(postvars):
credentials = auth.get_credentials()
self.send_response(303)
self.send_header('Location', '/rpSys.html')
self.send_header('Set-Cookie', 'C0=' + credentials['user'] + '; path=/')
self.send_header('Set-Cookie', 'C1=' + credentials['pass'] + '; path=/')
self.end_headers()
self.log_http(303, postvars)
else:
self.do_GET()
self.log_http(200, postvars)
else:
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.log_http(200, postvars)
示例13: do_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def do_POST(self):
"""Handle POST."""
if self.path != '/interact':
return self.respond({'status': 500})
ctype, pdict = cgi.parse_header(self.headers['content-type'])
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
postvars = cgi.parse_multipart(self.rfile, pdict)
model_response = self.interactive_running(postvars)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
json_str = json.dumps(model_response)
self.wfile.write(bytes(json_str, 'utf-8'))
示例14: test_attachments_create_on_task
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def test_attachments_create_on_task(self):
res = { "data": { "id": 5678, "name": "file.txt" } }
responses.add(POST, 'http://app/tasks/1337/attachments', status=200, body=json.dumps(res), match_querystring=True)
self.assertEqual(self.client.attachments.create_on_task(1337, 'file content', 'file name', 'file content-type'), res['data'])
request_content_type, pdict = cgi.parse_header(responses.calls[0].request.headers['Content-Type'])
self.assertEqual(request_content_type, 'multipart/form-data')
content_file = six.BytesIO(responses.calls[0].request.body)
multipart = cgi.parse_multipart(content_file, { 'boundary': six.b(pdict['boundary']) })
self.assertEqual(multipart['file'][0], six.b('file content'))
# TODO: verify filename and content-type, possibly using a different multipart decoder
示例15: parse_POST
# 需要導入模塊: import cgi [as 別名]
# 或者: from cgi import parse_multipart [as 別名]
def parse_POST(self):
ctype, pdict = parse_header(self.headers['content-type'])
pdict['boundary'] = str(pdict['boundary']).encode('utf-8')
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(
self.rfile.read(length),
keep_blank_values=1)
else:
postvars = {}
return postvars