Skip to content

Instantly share code, notes, and snippets.

@code2be
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code2be/220f7a1b55daa890afd3 to your computer and use it in GitHub Desktop.
Save code2be/220f7a1b55daa890afd3 to your computer and use it in GitHub Desktop.
Socket Server using Python
# http://facebook.com/code.we.bas
import socket
import sys
from thread import *
# Define HOST and PORT
HOST = '' # Host, Can be Empty to work on all available interfaces.
PORT = 8003 # Port to be used, must not be already used by another process.
# Create the Socket
our_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Try binding to HOST and PORT
try:
our_socket.bind((HOST, PORT))
except socket.error as error:
print 'Error #' + str(error[0]) + ': ' + error[1]
sys.exit()
# Begin Listenning
our_socket.listen(10)
def handle_connection(connection):
connection.send('Welcome to our Server ..\n')
while True:
in_msg = connection.recv(1024)
if not in_msg:
break
connection.send('You said: ' + in_msg)
connection.close()
# Keep trying to Accept any new connections ..
while 1:
connection, address = our_socket.accept()
print 'Connection from IP: ' + address[0] + ' on Port: ' + str(address[1])
start_new_thread(handle_connection, (connection,))
our_socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment