NDOJ/judge/bridge/echo_test_server.py

43 lines
1.1 KiB
Python
Raw Normal View History

2020-07-19 21:27:14 +00:00
from judge.bridge.base_handler import ZlibPacketHandler
class EchoPacketHandler(ZlibPacketHandler):
def on_connect(self):
2022-05-14 17:57:27 +00:00
print("New client:", self.client_address)
2020-07-19 21:27:14 +00:00
self.timeout = 5
def on_timeout(self):
2022-05-14 17:57:27 +00:00
print("Inactive client:", self.client_address)
2020-07-19 21:27:14 +00:00
def on_packet(self, data):
self.timeout = None
2022-05-14 17:57:27 +00:00
print(
"Data from %s: %r"
% (self.client_address, data[:30] if len(data) > 30 else data)
)
2020-07-19 21:27:14 +00:00
self.send(data)
def on_disconnect(self):
2022-05-14 17:57:27 +00:00
print("Closed client:", self.client_address)
2020-07-19 21:27:14 +00:00
def main():
import argparse
from judge.bridge.server import Server
parser = argparse.ArgumentParser()
2022-05-14 17:57:27 +00:00
parser.add_argument("-l", "--host", action="append")
parser.add_argument("-p", "--port", type=int, action="append")
parser.add_argument("-P", "--proxy", action="append")
2020-07-19 21:27:14 +00:00
args = parser.parse_args()
class Handler(EchoPacketHandler):
proxies = args.proxy or []
server = Server(list(zip(args.host, args.port)), Handler)
server.serve_forever()
2022-05-14 17:57:27 +00:00
if __name__ == "__main__":
2020-07-19 21:27:14 +00:00
main()