Tôi đang cố gắng tạo một máy chủ web python đơn giản để lưu văn bản được Post
chỉnh sửa thành tệp có tên store.json
nằm trong cùng thư mục với tập lệnh python. Đây là một nửa mã của tôi, ai đó có thể cho tôi biết phần còn lại nên là gì?Máy chủ web Python đơn giản để lưu tệp
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()
Nó nói 'ImportError: Không có module nào có tên 'BaseHTTPServer'' –
Ok, bạn cần thay đổi' từ BaseHTTPServer' thành 'từ http.server' –
Xin lỗi, không nhận thấy phần' -3.x' của thẻ. – gvalkov