Server Ports w/python

In working with EVE-NG I often find that I need specific ports open on a test server. Instead of always installing an application (eg NGINX) I hacked together a python that will open a port.


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$'])

PORT = int(raw_input("What port do you want the server to run on? "))

if 1 <= PORT <= 65535:
print " "
print " That is not a valid TCP port. "

HOST = '' # Hostname to bind

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

print "Port", PORT,"is open ... "
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break


Leave a Reply