1 Star 0 Fork 0

gsw945/new-site

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
python-server.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
gsw945 提交于 2017-08-29 19:54 . add files to remote
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import sys
import threading
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler
except ImportError:
from http.server import SimpleHTTPRequestHandler
try:
from BaseHTTPServer import HTTPServer
except ImportError:
from http.server import HTTPServer
ROOT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
# PUBLIC_RESOURCE_PREFIX = '/static'
PUBLIC_DIRECTORY = os.path.join(ROOT_DIRECTORY, 'content')
os.chdir(PUBLIC_DIRECTORY)
class MyRequestHandler(SimpleHTTPRequestHandler, object):
'''
def translate_path(self, path):
if self.path.startswith(PUBLIC_RESOURCE_PREFIX):
if self.path == PUBLIC_RESOURCE_PREFIX or self.path == PUBLIC_RESOURCE_PREFIX + '/':
return PUBLIC_DIRECTORY + '/index.html'
else:
return PUBLIC_DIRECTORY + path[len(PUBLIC_RESOURCE_PREFIX):]
else:
return super(MyRequestHandler, self).translate_path(path)
'''
pass
def run_server():
try:
server_address = ('0.0.0.0', 8998)
httpd = HTTPServer(server_address, MyRequestHandler)
print('visit by [http://{host}:{port}]'.format(
host=server_address[0],
port=server_address[1]
))
httpd.serve_forever()
except KeyboardInterrupt:
print('closing...')
finally:
httpd.server_close()
try:
thread = threading.Thread(None, run_server)
thread.daemon=True
thread.start()
while thread.isAlive():
thread.join(1)
# ... do things ...
# Shutdown server
# server.shutdown()
except (KeyboardInterrupt, SystemExit):
print('closing ...')
import time
time.sleep(3) # seconds
print('exit')
sys.exit(0)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gsw945/new-site.git
git@gitee.com:gsw945/new-site.git
gsw945
new-site
new-site
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385