1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # Standard loopback interface address (localhost)
6PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.bind((HOST, PORT))
10 s.listen()
11 conn, addr = s.accept()
12 with conn:
13 print('Connected by', addr)
14 while True:
15 data = conn.recv(1024)
16 if not data:
17 break
18 conn.sendall(data)
19
1import socket # Import socket module
2
3s = socket.socket() # Create a socket object
4host = socket.gethostname() # Get local machine name
5port = 12345 # Reserve a port for your service.
6s.bind((host, port)) # Bind to the port
7
8s.listen(5) # Now wait for client connection.
9while True:
10 c, addr = s.accept() # Establish connection with client.
11 print 'Got connection from', addr
12 c.send('Thank you for connecting')
13 c.close() # Close the connection
1import socket
2from socket import *
3import json
4
5serverPort = 6100
6serverSocket = socket(AF_INET, SOCK_STREAM)
7# Avoiding : Python [Errno 98] Address already in use
8
9
10serverSocket.bind(("", serverPort))
11serverSocket.listen(1)
12
13# TCP Server
14print("The server is listening on PORT 6100")
15
16while 1:
17
18 connectionSocket, addr = serverSocket.accept()
19 data = connectionSocket.recv(1024)
20
21 # De-serializing data
22 data_loaded = json.loads(data)
23
24 print("client Sent :\n ", data_loaded)
25
26 # Sending response
27 connectionSocket.send(str.encode("Mil gya data"))
28
29
30connectionSocket.close()