-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho-server.py
More file actions
40 lines (33 loc) · 948 Bytes
/
Copy pathecho-server.py
File metadata and controls
40 lines (33 loc) · 948 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
34
35
36
37
38
39
40
#! /usr/bin/env python
import sys
import socket
def handle_connection(sock):
print 'Handling connection...'
while 1:
try:
print 'Recieving data...'
data = sock.recv(4096)
print 'Data recieved...'
if not data:
break
print 'data:', (data,)
print 'Sending data...'
sock.sendall(data)
print 'Data sent...'
if '.\r\n' in data:
sock.close()
break
except socket.error:
break
if __name__ == '__main__':
interface, port = sys.argv[1:3]
port = int(port)
print 'binding', interface, port
sock = socket.socket()
sock.bind( (interface, port) )
sock.listen(5)
while 1:
print 'waiting...'
(client_sock, client_address) = sock.accept()
print 'got connection', client_address
handle_connection(client_sock)