我正在尝试制作一个简单的 python 网络服务器来将Post
编辑的文本保存到一个名为的文件store.json
中,该文件与 python 脚本位于同一文件夹中。这是我的一半代码,有人可以告诉我其余的应该是什么吗?
import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
#import pri
class StoreHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path == "/store.json":
f = open(curdir + sep + "store.json") #self.path has /test.html
self.send_response(200)
self.send_header('Content-type','text/json')
self.end_headers()
self.wfile.write(f.read())
f.close()
return
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
//if the url is 'store.json' then
//what do I do here?
def main():
try:
server = HTTPServer(('', 80), StoreHandler)
print 'started...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()