Last active
August 29, 2015 14:07
-
-
Save bennettaur/0677864b25d0dc5b2e5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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