Skip to content

Instantly share code, notes, and snippets.

@loitho
Created January 20, 2020 10:26
Show Gist options
  • Save loitho/a24b2cf93191f053ec2d62907a8f47eb to your computer and use it in GitHub Desktop.
Save loitho/a24b2cf93191f053ec2d62907a8f47eb to your computer and use it in GitHub Desktop.
Simple python server listener to test port
#!/usr/bin/env python2
## Run with ./program.py <port number>
import socket
import sys
HOST = '' # Symbolic name, meaning all available interfaces
PORT = int(sys.argv[1]) # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment