python: add initial support for Python

Signed-off-by: Shayne Sweeney <shayne@tailscale.com>
This commit is contained in:
Shayne Sweeney
2023-03-04 22:26:14 -05:00
committed by shayne
parent 7bb5e96b97
commit 2df6a30f8a
12 changed files with 529 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
# Copyright (c) Tailscale Inc & AUTHORS
# SPDX-License-Identifier: BSD-3-Clause
# TODO(shayne): proper select/poll/epoll + os.set_blocking(conn, False)
import os
import select
from tailscale import TSNet
def handler(conn):
while True:
r, _, _ = select.select([conn], [], [], 10)
if not conn in r:
os._exit(0)
data = os.read(conn, 2048)
print(data.decode(), end="")
def main():
procs = []
ts = TSNet(ephemeral=True)
ts.up()
ln = ts.listen("tcp", ":1999")
while True:
while procs:
pid, exit_code = os.waitpid(-1, os.WNOHANG)
if pid == 0:
break
procs.remove(pid)
conn = ln.accept()
pid = os.fork()
if pid == 0:
return handler(conn)
procs.append(pid)
ln.close()
ts.close()
if __name__ == "__main__":
main()