-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-by-port.py
More file actions
31 lines (25 loc) · 883 Bytes
/
server-by-port.py
File metadata and controls
31 lines (25 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python
# Demo server to open a port
# Modified from Python tutorial docs
import socket
import subprocess
print "---------------------------------------------------------"
print "This script will open any TCP port that is not currently"
print "in use. Below are the ports in use. Any port < 1024 will"
print "require sudo."
print "---------------------------------------------------------"
print " "
# subprocess.check_output(['netstat', '-tulpn', '| grep tcp$'])
what_port = int(raw_input("What port do you want the server to run on? "))
HOST = '127.0.0.1' # Hostname to bind
PORT = what_port # Open non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()