[英]Python 2 do_POST http.server multipart/formdata to Python 3
問題:試圖將講師的python 2代碼翻譯成python 3
具體問題:無法從python 3中的表單訪問消息字段
def do_POST(self):
try:
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
ctype, pdict = cgi.parse_header(
self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, pdict)
messagecontent = fields.get('message')
output = ""
output += "<html><body>"
output += " <h2> Okay, how about this: </h2>"
output += "<h1> %s </h1>" % messagecontent[0]
output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
output += "</body></html>"
self.wfile.write(output)
print output
except:
pass
在查找文檔、github 存儲庫、stackoverflow 帖子並花費了無數個小時之后......我無法弄清楚如何在 python 3 中提取消息字段,如fields.get('message')
。
def do_POST(self):
try:
length = int(self.headers['Content-Length'])
print(self.headers['Content-Type'])
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
post_data = parse_qs(self.rfile.read(length).decode('utf-8'))
self.wfile.write("Lorem Ipsum".encode("utf-8"))
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, pdict)
messagecontent = fields.get('message')
output = ''
output += '<html><body>'
output += '<h2> Okay, how about this: </h2>'
output += '<h1> %s </h1>' % messagecontent[0]
# You now have a dictionary of the post data
output += "<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += '</html></body>'
self.wfile.write(output.encode('utf-8'))
except:
print('Error!')
我的 post_data 變量是一個字典,但我找不到一種方法來提取我在表單中輸入的“嗨”消息。 我也不確定這是否是從表單中提取數據的正確方法。
>>> post_data
{' name': ['"message"\r\n\r\nhi\r\n------WebKitFormBoundarygm0MsepKJXVrBubX--\r\n']}
def do_POST(self):
try:
length = int(self.headers['Content-Length'])
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
post_data = parse_qs(self.rfile.read(length).decode('utf-8'))
messagecontent = post_data.get(' name')[0].split('\n')[2]
output = ''
output += '<html><body>'
output += '<h2> Okay, how about this: </h2>'
output += '<h1> %s </h1>' % messagecontent
output += "<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += '</html></body>'
self.wfile.write(output.encode('utf-8'))
except:
pass
如果有更好的方法,我想知道! 不知道為什么我必須在post_data.get(' name')
之前添加一個空格。 但是,嘿! 它有效!
def do_POST(self):
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
if ctype == 'multipart/form-data':
pdict['boundary'] = bytes(pdict['boundary'], 'utf-8')
fields = cgi.parse_multipart(self.rfile, pdict)
messagecontent = fields.get('message')[0].decode('utf-8')
output = ''
output += '<html><body>'
output += '<h2> Okay, how about this: </h2>'
output += '<h1> %s </h1>' % messagecontent
output += "<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += '</html></body>'
self.wfile.write(output.encode('utf-8'))
使用它可以在 Udacity 全棧基礎課程中解脫!
我無法使您的代碼在發布時(2019 年)工作。 檢查 cgi 模塊, parse_multipart
嘗試訪問pdict['CONTENT-LENGHT']
。
我的解決方案,工作@發布時間:
def do_POST(self):
try:
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
# HEADERS are now in dict/json style container
ctype, pdict = cgi.parse_header(
self.headers.get('content-type'))
# boundary data needs to be encoded in a binary format
if ctype == 'multipart/form-data':
pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
pdict['CONTENT-LENGTH'] = self.headers.get('content-length')
fields = cgi.parse_multipart(self.rfile, pdict)
messagecontent = fields.get('message')
output = ""
output += "<html><body>"
output += " <h2> Okay, how about this: </h2>"
# decode it back into a string rather than byte string(b'stuff')
output += "<h1> {} </h1>".format(messagecontent[0])
output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
output += "</body></html>"
self.wfile.write(output.encode())
print(output)
except:
raise
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.