Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eca991d

Browse files
committed
Change security policy -- only accept requests from current host.
1 parent d1bef00 commit eca991d

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

Demo/pysvr/pysvr.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
77
Usage: pysvr.py [port]
88
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+
913
"""
1014

1115
import sys, os, string, getopt, thread, socket, traceback
1216

13-
OK_DOMAINS = [".cnri.reston.va.us", ".python.org"]
14-
15-
PORT = 7585892 % 0xFFFF # == 49367
17+
PORT = 4000 # Default port
1618

1719
def main():
1820
try:
@@ -43,33 +45,19 @@ def main_thread(port):
4345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4446
sock.bind(("", port))
4547
sock.listen(5)
48+
print "Listening on port", port, "..."
4649
while 1:
4750
(conn, addr) = sock.accept()
4851
thread.start_new_thread(service_thread, (conn, addr))
4952
del conn, addr
5053

5154
def service_thread(conn, addr):
5255
(caddr, cport) = addr
53-
try:
54-
host, aliases, ipaddrs = socket.gethostbyaddr(caddr)
55-
except socket.error:
56-
print "Don't know hostname for", caddr
57-
return
58-
if '.' not in host:
59-
for a in aliases:
60-
if '.' in a:
61-
host = a
62-
break
63-
else:
64-
print "Only a local name (%s) for %s" % (host, caddr)
65-
return
66-
i = string.find(host, '.')
67-
domain = string.lower(host[i:])
68-
if domain not in OK_DOMAINS:
69-
print "Connection from", host, "not accepted"
56+
if caddr != socket.gethostbyname(socket.gethostname()):
57+
print "Connection from", caddr, "not accepted."
7058
return
7159
print "Thread %s has connection from %s.\n" % (str(thread.get_ident()),
72-
host),
60+
caddr),
7361
stdin = conn.makefile("r")
7462
stdout = conn.makefile("w", 0)
7563
run_interpreter(stdin, stdout)

0 commit comments

Comments
 (0)