我制作了如下的简单网络服务器。
import BaseHTTPServer, os, cgi
import cgitb; cgitb.enable()
html = """
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
File upload: <input type="file" name="upfile">
<input type="submit" value="upload">
</form>
</body>
</html>
"""
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("content-type", "text/html;charset=utf-8")
self.end_headers()
self.wfile.write(html)
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query = cgi.parse_multipart(self.rfile, pdict)
upfilecontent = query.get('upfile')
if upfilecontent:
# i don't know how to get the file name.. so i named it 'tmp.dat'
fout = file(os.path.join('tmp', 'tmp.dat'), 'wb')
fout.write (upfilecontent[0])
fout.close()
self.do_GET()
if __name__ == '__main__':
server = BaseHTTPServer.HTTPServer(("127.0.0.1", 8080), Handler)
print('web server on 8080..')
server.serve_forever()
在BaseHTTPRequestHandler的do_Post方法中,我成功获得了上传的文件数据。
但我无法弄清楚如何获取上传文件的原始名称。 self.rfile.name只是一个'socket' 如何获取上传的文件名?
答案 0 :(得分:2)
你在那里使用相当破碎的代码作为起点(例如,查看使用名称global rootnode
的{{1}} 无处 - 显然是半编辑的源代码,而且非常糟糕。
无论如何,您使用rootnode
的“客户端”是什么形式?它是如何设置POST
字段的?
为什么不使用Python's docs中记录的正常upfile
方法?这样,您可以使用相应字段的FieldStorage
属性来获取要读取的类文件对象,或者使用其.file
属性将其全部读取到内存中并将其作为字符串获取,再加上该字段的.value
属性用于了解上传文件的名称。 .filename
上更详细但简洁的文档是here。
修改:既然OP编辑了Q以澄清问题,我发现问题:FieldStorage
不根据CGI规范设置环境,因此BaseHTTPServer
模块不是很有用。不幸的是,环境设置的唯一简单方法是从cgi
窃取和破解大部分代码(不是为了重用,而是需要,叹息,复制和粘贴编码),例如......:
CGIHTTPServer.py
这可以进一步大幅简化,但不能花费一些时间和精力完成这项任务: - (。
有了这个def populenv(self):
path = self.path
dir, rest = '.', 'ciao'
# find an explicit query string, if present.
i = rest.rfind('?')
if i >= 0:
rest, query = rest[:i], rest[i+1:]
else:
query = ''
# dissect the part after the directory name into a script name &
# a possible additional path, to be stored in PATH_INFO.
i = rest.find('/')
if i >= 0:
script, rest = rest[:i], rest[i:]
else:
script, rest = rest, ''
# Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
# XXX Much of the following could be prepared ahead of time!
env = {}
env['SERVER_SOFTWARE'] = self.version_string()
env['SERVER_NAME'] = self.server.server_name
env['GATEWAY_INTERFACE'] = 'CGI/1.1'
env['SERVER_PROTOCOL'] = self.protocol_version
env['SERVER_PORT'] = str(self.server.server_port)
env['REQUEST_METHOD'] = self.command
uqrest = urllib.unquote(rest)
env['PATH_INFO'] = uqrest
env['SCRIPT_NAME'] = 'ciao'
if query:
env['QUERY_STRING'] = query
host = self.address_string()
if host != self.client_address[0]:
env['REMOTE_HOST'] = host
env['REMOTE_ADDR'] = self.client_address[0]
authorization = self.headers.getheader("authorization")
if authorization:
authorization = authorization.split()
if len(authorization) == 2:
import base64, binascii
env['AUTH_TYPE'] = authorization[0]
if authorization[0].lower() == "basic":
try:
authorization = base64.decodestring(authorization[1])
except binascii.Error:
pass
else:
authorization = authorization.split(':')
if len(authorization) == 2:
env['REMOTE_USER'] = authorization[0]
# XXX REMOTE_IDENT
if self.headers.typeheader is None:
env['CONTENT_TYPE'] = self.headers.type
else:
env['CONTENT_TYPE'] = self.headers.typeheader
length = self.headers.getheader('content-length')
if length:
env['CONTENT_LENGTH'] = length
referer = self.headers.getheader('referer')
if referer:
env['HTTP_REFERER'] = referer
accept = []
for line in self.headers.getallmatchingheaders('accept'):
if line[:1] in "\t\n\r ":
accept.append(line.strip())
else:
accept = accept + line[7:].split(',')
env['HTTP_ACCEPT'] = ','.join(accept)
ua = self.headers.getheader('user-agent')
if ua:
env['HTTP_USER_AGENT'] = ua
co = filter(None, self.headers.getheaders('cookie'))
if co:
env['HTTP_COOKIE'] = ', '.join(co)
# XXX Other HTTP_* headers
# Since we're setting the env in the parent, provide empty
# values to override previously set values
for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
env.setdefault(k, "")
os.environ.update(env)
函数,我们可以重新编码:
populenv
......从此过上幸福的生活;-)。 (当然,使用任何体面的WSGI服务器,甚至是the demo one都会容易得多,但是这个练习 对CGI及其内部结构具有启发意义; - )。
答案 1 :(得分:1)
通过使用cgi.FieldStorage,您可以轻松提取文件名。请查看以下示例:
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], })
filename = form['upfile'].filename
data = form['upfile'].file.read()
open("./%s"%filename, "wb").write(data)
self.do_GET()
答案 2 :(得分:0)
...或使用您自己的cgi.parse_multipart版本,尤其是解决此问题:
# my fix: prefer 'filename' over 'name' field!
if 'filename' in params:
name = params['filename']
name = os.path.basename(name) # Edge, IE return abs path!
elif 'name' in params:
name = params['name']
else:
continue