-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecho_server.py
More file actions
26 lines (20 loc) · 772 Bytes
/
echo_server.py
File metadata and controls
26 lines (20 loc) · 772 Bytes
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
#!/usr/bin/env python
import socket
host = '' # listen on all connections (WiFi, etc)
port = 50000
backlog = 5 # how many connections can we stack up
size = 1024 # number of bytes to receive at once
## create the socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set an option to tell the OS to re-use the socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# the bind makes it a server
s.bind( (host,port) )
s.listen(backlog)
while True: # keep looking for new connections forever
client, address = s.accept() # look for a connection
data = client.recv(size)
if data: # if the connection was closed there would be no data
print "received: %s, sending it back"%data
client.send(data)
client.close()