1#!/usr/bin/python # This is server.py file
2
3import socket # Import socket module
4
5s = socket.socket() # Create a socket object
6host = socket.gethostname() # Get local machine name
7port = 12345 # Reserve a port for your service.
8s.bind((host, port)) # Bind to the port
9
10s.listen(5) # Now wait for client connection.
11while True:
12 c, addr = s.accept() # Establish connection with client.
13 print 'Got connection from', addr
14 c.send('Thank you for connecting')
15 c.close() # Close the connection
1import socket
2import os
3
4sock_file = "/tmp/python_socket"
5
6if os.path.exists(sock_file):
7 os.remove(sock_file)
8
9sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
10sock.bind(sock_file)
11sock.listen()
12print("Listening...")
13while True:
14 conn, _addr = sock.accept()
15 datagram = conn.recv(1024)
16 request = datagram.decode('utf-8')
17 conn.send(str("Back at you:"+request).encode('utf-8'))