Skip to content

Instantly share code, notes, and snippets.

@divyanshu013
Last active September 30, 2015 20:41
Show Gist options
  • Save divyanshu013/3d4fe7266deef6f57607 to your computer and use it in GitHub Desktop.
Save divyanshu013/3d4fe7266deef6f57607 to your computer and use it in GitHub Desktop.
A simple server-client application in Python
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close() # Close the socket when done
#!/usr/bin/python # This is server.py file
import socket # Import socket module
server = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
server.bind((host, port)) # Bind to the port
server.listen(5) # Now wait for client connection.
while True:
client, addr = server.accept() # Establish connection with client.
print 'Got connection from', addr
client.send('Connection closed')
client.close() # Close the connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment