-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_server.py
More file actions
37 lines (30 loc) · 1.11 KB
/
tcp_server.py
File metadata and controls
37 lines (30 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import socket
def start_tcp_server():
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket._SOCK_STREAM)
# Bind the socket to the address and port
server_address = ('localhost', 65432)
print('Starting up on {} port {}'.format(*server_address))
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
while True:
# Wait for connection
print('Waiting for a connection')
connection, client_address = server_socket.accept()
try:
print('Connection from', client_address)
# Receive the data in small chunks and retransmit
while True:
data = connection.recv(16)
print('Received {!r}').format(data)
if data:
print('Sending data back to the client')
connection.sendall(data)
else:
print('No mmore data from', client_address)
break
finally:
# Clean up connection
connection.close()
start_tcp_server()