Python Web服务器文件输出

Python Web服务器文件输出,python,webserver,Python,Webserver,我有一个pythonweb服务器正在运行,它会将文件正确地输出到我的目录中,但是我不知道如何附加到已经存在的文件中,或者如何将时间戳附加到文件中 f_path = open(os.path.join('/Inet', 'video.mov'), 'a+') f_path.write(qs) 问题是,如果文件已经存在,它将覆盖它,而不是创建新文件。因此,我想: 将日期和时间戳附加到文件名或 只需将新数据附加到现有文件中。 另外,让它自动检测服务器IP地址也很方便 import BaseHTTPS

我有一个pythonweb服务器正在运行,它会将文件正确地输出到我的目录中,但是我不知道如何附加到已经存在的文件中,或者如何将时间戳附加到文件中

f_path = open(os.path.join('/Inet', 'video.mov'), 'a+')
f_path.write(qs)
问题是,如果文件已经存在,它将覆盖它,而不是创建新文件。因此,我想:

将日期和时间戳附加到文件名或 只需将新数据附加到现有文件中。 另外,让它自动检测服务器IP地址也很方便

import BaseHTTPServer, os, cgi
import cgitb; cgitb.enable()
import time
import datetime

timeStamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
print timeStamp

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("Upload Complete")

def do_POST(self):

    print self.headers
    form = cgi.FieldStorage(fp = self.rfile)
    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
    length = cgi.parse_header(self.headers.getheader('Content-Length'))
    print length[0]
    if ctype == 'application/x-www-form-urlencoded':
        qs = self.rfile.read(int(length[0]))
        fout = file(os.path.join('/Inet', 'video.mov'), 'wb')
        fout.write (qs)
        fout.close()
    self.do_GET()

if __name__ == '__main__':
    server = BaseHTTPServer.HTTPServer(("192.168.1.10", 8000), Handler)
    print('web server on 8000..')
    server.serve_forever()

您正在使用“wb”过度写入现有文件。相反,您可以先用append“a”打开文件,然后写入文件

f_path = open(os.path.join('/Inet', 'video.mov'), 'a+')
f_path.write(qs)

我可以用这个附加日期和时间

timeStamp = datetime.datetime.now().strftime("%A, %d. %B %Y %I-%M%p")
qs = self.rfile.read(int(length[0]))
fout = file(os.path.join('/Inet', 'video_'+timeStamp+'_.mov'), 'wb')
fout.write (qs)

与open一起使用。。。对于文件,请使用“a”进行附加。我尝试了此操作,但由于某些原因,我无法获得正确的语法。我进行了更改,文件也可以附加,但视频无法播放,并且第二次上载请求的语法错误为400。