| 1 | #! /usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | """A multi-threaded telnet-like server that gives a Python prompt.
|
|---|
| 4 |
|
|---|
| 5 | This is really a prototype for the same thing in C.
|
|---|
| 6 |
|
|---|
| 7 | Usage: pysvr.py [port]
|
|---|
| 8 |
|
|---|
| 9 | For security reasons, it only accepts requests from the current host.
|
|---|
| 10 | This can still be insecure, but restricts violations from people who
|
|---|
| 11 | can log in on your machine. Use with caution!
|
|---|
| 12 |
|
|---|
| 13 | """
|
|---|
| 14 |
|
|---|
| 15 | import sys, os, string, getopt, thread, socket, traceback
|
|---|
| 16 |
|
|---|
| 17 | PORT = 4000 # Default port
|
|---|
| 18 |
|
|---|
| 19 | def 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 |
|
|---|
| 37 | def usage(msg=None):
|
|---|
| 38 | sys.stdout = sys.stderr
|
|---|
| 39 | if msg:
|
|---|
| 40 | print msg
|
|---|
| 41 | print "\n", __doc__,
|
|---|
| 42 | sys.exit(2)
|
|---|
| 43 |
|
|---|
| 44 | def 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]:
|
|---|
|
|---|