如果您阅读了源代码,您将看到只能在命令行上覆盖端口。如果你想改变它在服务的主机,你将需要实现
test()的方法
SimpleHTTPServer和
baseHTTPServer自己。但这应该很容易。
这是您可以轻松实现的方法:
import sysfrom SimpleHTTPServer import SimpleHTTPRequestHandlerimport baseHTTPServerdef test(HandlerClass=SimpleHTTPRequestHandler, ServerClass=baseHTTPServer.HTTPServer): protocol = "HTTP/1.0" host = '' port = 8000 if len(sys.argv) > 1: arg = sys.argv[1] if ':' in arg: host, port = arg.split(':') port = int(port) else: try: port = int(sys.argv[1]) except: host = sys.argv[1] server_address = (host, port) HandlerClass.protocol_version = protocol httpd = ServerClass(server_address, HandlerClass) sa = httpd.socket.getsockname() print "Serving HTTP on", sa[0], "port", sa[1], "..." httpd.serve_forever()if __name__ == "__main__": test()
并使用它:
> python server.py 127.0.0.1 Serving HTTP on 127.0.0.1 port 8000 ...> python server.py 127.0.0.1:9000Serving HTTP on 127.0.0.1 port 9000 ...> python server.py 8080 Serving HTTP on 0.0.0.0 port 8080 ...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)