Skip to content

Instantly share code, notes, and snippets.

@MrVaughan
Forked from bennettaur/simple_socket_server.py
Last active August 29, 2015 14:08
Show Gist options
  • Save MrVaughan/15179712f0bc681387e9 to your computer and use it in GitHub Desktop.
Save MrVaughan/15179712f0bc681387e9 to your computer and use it in GitHub Desktop.
Simple listening server to confirm callback connections
#Listening Server to confirm callback connections
#If running this on nix based you may need to open up an iptables rule to open that port on the network.
#iptables command:
# sudo iptables -I INPUT -p tcp --dport 8888 -j ACCEPT
#To remove the iptables rule after you are finished:
# sudo iptables -D INPUT -p tcp --dport 8888 -j ACCEPT
import logging
import socket
import sys
logger = logging.getLogger("connections.log")
# Find the current host's name and IP
HOST = socket.gethostbyname(gethostname())
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error as msg:
loggeer.exception('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
#Start listening on socket
s.listen(10)
msg = 'Listening on port {}'.format(PORT)
logger.log(msg)
print msg
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
logger.log('New connection: {}:{}'.format(addr[0], str(addr[1]))
conn.close()
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment