source: vendor/python/2.5/Demo/pysvr/pysvr.py

Last change on this file was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 3.3 KB
Line 
1#! /usr/bin/env python
2
3"""A multi-threaded telnet-like server that gives a Python prompt.
4
5This is really a prototype for the same thing in C.
6
7Usage: pysvr.py [port]
8
9For security reasons, it only accepts requests from the current host.
10This can still be insecure, but restricts violations from people who
11can log in on your machine. Use with caution!
12
13"""
14
15import sys, os, string, getopt, thread, socket, traceback
16
17PORT = 4000 # Default port
18
19def main():
20 try:
21 opts, args = getopt.getopt(sys.argv[1:], "")
22 if len(args) > 1:
23 raise getopt.error, "Too many arguments."
24 except getopt.error, msg:
25 usage(msg)
26 for o, a in opts:
27 pass
28 if args:
29 try:
30 port = string.atoi(args[0])
31 except ValueError, msg:
32 usage(msg)
33 else:
34 port = PORT
35 main_thread(port)
36
37def usage(msg=None):
38 sys.stdout = sys.stderr
39 if msg:
40 print msg
41 print "\n", __doc__,
42 sys.exit(2)
43
44def main_thread(port):
45 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46 sock.bind(("", port))
47 sock.listen(5)
48 print "Listening on port", port, "..."
49 while 1:
50 (conn, addr) = sock.accept()
51 if addr[0] != conn.getsockname()[0]: