Python BaseHTTPServer,如何将多个主机重定向到 ip/port?
Python BaseHTTPServer, how to redirect multiple hosts to ip/port?
我目前有以下代码:
from BaseHTTPServer import BaseHTTPRequestHandler
from pathlib import Path
from random import randint
import json
import random
example = 'logs/example.json'
class GetHandler(BaseHTTPRequestHandler):
# Slurp data from file
def dummy_data(self):
json_result = Path(example)
if json_result.is_file():
return json.load(open(example))
# Return data or empty
def random_selection(self):
data = self.dummy_data()
try:
return random.sample(data, randint(1, len(data)+50))
# Purposefully introduce entropy
except ValueError:
return ''
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(self.random_selection()))
return
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer(('localhost', 8080), GetHandler)
print 'Starting server at http://127.0.0.1:8080'
server.serve_forever()
我已经对 /etc/hosts
进行了如下修补:
server-0001 server-0002 server-0003 server-0004 127.0.0.1
我正在寻找让服务器 0001-4 重定向到 127.0.0.1:8080
的方法,但我不知道如何做?这与/etc/resolv.conf
有关吗?我正在使用 OSX 但希望任何 *nix 解决方案都应该工作我希望(除非 ipfw 显然,因为我们不再像理智的人那样拥有它)。
看起来最明智的答案是修补 /etc/hosts
,然后将 BaseHTTPServer
的端口更改为 80
。这并没有解决最初的问题,而是一个公平的妥协。
修补脚本:
#!/bin/bash
# ensure running as root
if [ "$(id -u)" != "0" ] ; then
exec sudo "[=10=]" "$@"
fi
if ! grep "^#- marker " /etc/hosts ; then
echo -e '\n##+ marker' >> /etc/hosts
while read server ; do
[ 'x' != "${server}x" ] && echo -e "127.0.0.1 ${server}.io www.${server}.io" >> /etc/hosts
done <read/servers.txt
echo -e '\n##- marker' >> /etc/hosts
fi
修改为Python
....
def main():
try:
server = HTTPServer(('', 80), GetHandler)
print '->> Starting server at http://127.0.0.1:80 and all patched hosts'
server.serve_forever()
except KeyboardInterrupt:
print '->> Interrupt received; closing server socket'
server.socket.close()
if __name__ == '__main__':
main()
我目前有以下代码:
from BaseHTTPServer import BaseHTTPRequestHandler
from pathlib import Path
from random import randint
import json
import random
example = 'logs/example.json'
class GetHandler(BaseHTTPRequestHandler):
# Slurp data from file
def dummy_data(self):
json_result = Path(example)
if json_result.is_file():
return json.load(open(example))
# Return data or empty
def random_selection(self):
data = self.dummy_data()
try:
return random.sample(data, randint(1, len(data)+50))
# Purposefully introduce entropy
except ValueError:
return ''
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(self.random_selection()))
return
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer(('localhost', 8080), GetHandler)
print 'Starting server at http://127.0.0.1:8080'
server.serve_forever()
我已经对 /etc/hosts
进行了如下修补:
server-0001 server-0002 server-0003 server-0004 127.0.0.1
我正在寻找让服务器 0001-4 重定向到 127.0.0.1:8080
的方法,但我不知道如何做?这与/etc/resolv.conf
有关吗?我正在使用 OSX 但希望任何 *nix 解决方案都应该工作我希望(除非 ipfw 显然,因为我们不再像理智的人那样拥有它)。
看起来最明智的答案是修补 /etc/hosts
,然后将 BaseHTTPServer
的端口更改为 80
。这并没有解决最初的问题,而是一个公平的妥协。
修补脚本:
#!/bin/bash
# ensure running as root
if [ "$(id -u)" != "0" ] ; then
exec sudo "[=10=]" "$@"
fi
if ! grep "^#- marker " /etc/hosts ; then
echo -e '\n##+ marker' >> /etc/hosts
while read server ; do
[ 'x' != "${server}x" ] && echo -e "127.0.0.1 ${server}.io www.${server}.io" >> /etc/hosts
done <read/servers.txt
echo -e '\n##- marker' >> /etc/hosts
fi
修改为Python
....
def main():
try:
server = HTTPServer(('', 80), GetHandler)
print '->> Starting server at http://127.0.0.1:80 and all patched hosts'
server.serve_forever()
except KeyboardInterrupt:
print '->> Interrupt received; closing server socket'
server.socket.close()
if __name__ == '__main__':
main()