Python SimpleHTTPServer 更改服务目录
Python SimpleHTTPServer Change Service Directory
我编写了以下代码来启动 HTTP 服务器,将来可以选择启动 TCP/IP 服务器:
import SimpleHTTPServer
import SocketServer
import time
import socket
def choose():
if raw_input("Would you like to start an HTTP or TCP/IP Server?: ") == "HTTP":
print "You have selected HTTP server."
if raw_input("Is this correct? Use Y/N to answer. ") == "Y":
print ""
start_HTTP()
else:
choose()
else:
print "Goodbye! "
def start_HTTP():
PORT = int(raw_input("Which port do you want to send out of? "))
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Please wait one moment..."
time.sleep(2)
run_HTTP(PORT, httpd)
def run_HTTP(PORT, httpd):
print "Use the following IP address to connect to this server (LAN only): " + [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] #prints local IP address
print "Now serving from port: ", PORT
print "To shutdown the server, use the PiServer_Off software."
time.sleep(2)
print ""
print "Any traffic through the server will be recorded and displayed below: "
httpd.serve_forever()
choose()
我想更改目录,以便在将来除了主机之外没有人可以终止服务器(因为 PiServer Off 软件将安装在同一目录中)。
我找到了这个解决方案,但它看起来适用于 shell,我不知道如何针对我的代码修改它(使用 Pycharm):http://www.tecmint.com/python-simplehttpserver-to-create-webserver-or-serve-files-instantly/
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
我也找到了这个,但它似乎没有回答我的问题:
我想知道是否有人可以分享和解释他们在不移动服务器文件的情况下更改目录的方法,因为我想用它来构建一个家庭文件共享系统。
谢谢!
使用 os.chdir
更改当前目录,然后像往常一样启动服务器。
问题 基本上是这个问题的重复。
不过那边Andy Hayden提出的方案好像比较合适
(它较少 'hacky'/ 依赖于副作用,而是使用 class 构造函数。)
是这样的:
import http.server
import socketserver
PORT = 8000
DIRECTORY = "web"
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
上面的代码适用于 Python >= 3.6
对于 Python 到 3.5,没有可用于 TCPServer 的基础 class 的上下文管理器协议,但这仅意味着您需要更改 with
语句并将其转换为普通赋值:
httpd = socketserver.TCPServer(("", PORT), Handler)
感谢 Anthony Sottile for this 。
我编写了以下代码来启动 HTTP 服务器,将来可以选择启动 TCP/IP 服务器:
import SimpleHTTPServer
import SocketServer
import time
import socket
def choose():
if raw_input("Would you like to start an HTTP or TCP/IP Server?: ") == "HTTP":
print "You have selected HTTP server."
if raw_input("Is this correct? Use Y/N to answer. ") == "Y":
print ""
start_HTTP()
else:
choose()
else:
print "Goodbye! "
def start_HTTP():
PORT = int(raw_input("Which port do you want to send out of? "))
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Please wait one moment..."
time.sleep(2)
run_HTTP(PORT, httpd)
def run_HTTP(PORT, httpd):
print "Use the following IP address to connect to this server (LAN only): " + [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] #prints local IP address
print "Now serving from port: ", PORT
print "To shutdown the server, use the PiServer_Off software."
time.sleep(2)
print ""
print "Any traffic through the server will be recorded and displayed below: "
httpd.serve_forever()
choose()
我想更改目录,以便在将来除了主机之外没有人可以终止服务器(因为 PiServer Off 软件将安装在同一目录中)。
我找到了这个解决方案,但它看起来适用于 shell,我不知道如何针对我的代码修改它(使用 Pycharm):http://www.tecmint.com/python-simplehttpserver-to-create-webserver-or-serve-files-instantly/
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
我也找到了这个,但它似乎没有回答我的问题:
我想知道是否有人可以分享和解释他们在不移动服务器文件的情况下更改目录的方法,因为我想用它来构建一个家庭文件共享系统。
谢谢!
使用 os.chdir
更改当前目录,然后像往常一样启动服务器。
问题
不过那边Andy Hayden提出的方案好像比较合适
(它较少 'hacky'/ 依赖于副作用,而是使用 class 构造函数。)
是这样的:
import http.server
import socketserver
PORT = 8000
DIRECTORY = "web"
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
上面的代码适用于 Python >= 3.6
对于 Python 到 3.5,没有可用于 TCPServer 的基础 class 的上下文管理器协议,但这仅意味着您需要更改 with
语句并将其转换为普通赋值:
httpd = socketserver.TCPServer(("", PORT), Handler)
感谢 Anthony Sottile for this