-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.py
More file actions
33 lines (26 loc) · 837 Bytes
/
echo_server.py
File metadata and controls
33 lines (26 loc) · 837 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
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import socket
import time
#define address & buffer size
HOST = ""
PORT = 8001
BUFFER_SIZE = 1024
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
#QUESTION 3
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#bind socket to address
s.bind((HOST, PORT))
#set to listening mode
s.listen(2)
#continuously listen for connections
while True:
conn, addr = s.accept()
print("Connected by", addr)
#recieve data, wait a bit, then send it back
full_data = conn.recv(BUFFER_SIZE)
time.sleep(0.5)
conn.sendall(full_data)
conn.close()
if __name__ == "__main__":
main()