1import select
2import socket
3import sys
4import Queue
5
6# Create a TCP/IP socket
7server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8server.setblocking(0)
9
10# Bind the socket to the port
11server_addr = ('localhost', 10000)
12print('starting server on {server_addr[0]}:{server_addr[1]}')
13server.bind(server_address)
14server.listen(5)
15
16# Sockets from which we expect to read
17inputs = [ server ]
18
19# Sockets to which we expect to write
20outputs = [ ]
21
22# Outgoing message queues (socket:Queue)
23message_queues = {}
24
25while inputs:
26
27 # Wait for at least one of the sockets to be ready for processing
28 print('waiting for the next event')
29 readable, writable, exceptional = select.select(inputs, outputs, inputs)
30
31 # Handle inputs
32 for s in readable:
33 if s is server:
34 # A "readable" server socket is ready to accept a connection
35 connection, client_address = s.accept()
36 print(f'new connection from {client_address}')
37 connection.setblocking(False)
38 inputs.append(connection)
39
40 # Give the connection a queue for data we want to send
41 message_queues[connection] = Queue.Queue()
42 else:
43 data = s.recv(1024)
44 if data:
45 # A readable client socket has data
46 print(f'received "{data}" from {s.getpeername()}')
47 message_queues[s].put(data)
48 # Add output channel for response
49 if s not in outputs:
50 outputs.append(s)
51 else:
52 # Interpret empty result as closed connection
53 print(f'closing {client_address} after reading no data')
54 # Stop listening for input on the connection
55 if s in outputs:
56 outputs.remove(s)
57 inputs.remove(s)
58 s.close()
59
60 # Remove message queue
61 del message_queues[s]
62
63 # Handle outputs
64 for s in writable:
65 try:
66 next_msg = message_queues[s].get_nowait()
67 except Queue.Empty:
68 # No messages waiting so stop checking for writability.
69 print(f'output queue for {s.getpeername()} is empty')
70 outputs.remove(s)
71 else:
72 print(f'sending "{next_msg}" to {s.getpeername()}')
73 s.send(next_msg)
74
75 # Handle "exceptional conditions"
76 for s in exceptional:
77 print(f'handling exceptional condition for {s.getpeername()}')
78 # Stop listening for input on the connection
79 inputs.remove(s)
80 if s in outputs:
81 outputs.remove(s)
82 s.close()
83 # Remove message queue
84 del message_queues[s]
1import socket
2
3HOST = '127.0.0.1' # Standard loopback interface address (localhost)
4PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
5
6with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
7 s.bind((HOST, PORT))
8 s.listen()
9 conn, addr = s.accept()
10 with conn:
11 print('Connected by', addr)
12 while True:
13 data = conn.recv(1024)
14 if not data:
15 break
16 conn.sendall(data)
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()