-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcpServerDummy.py
40 lines (34 loc) · 1.07 KB
/
tcpServerDummy.py
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
# Symbolic name, meaning all available interfaces
VAR_HOST = ''
# Arbitrary non-privileged port
VAR_PORT = 9999
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
local_hostname = socket.gethostname()
local_ip = socket.gethostbyname(local_hostname)
# Bind socket to local host and port
try:
s.bind((VAR_HOST, VAR_PORT))
except socket.error as msg:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
# Start listening on socket
s.listen(10)
print('Socket now listening on ' + local_ip + '/' + local_hostname + ' on port ' + str(VAR_PORT))
# Now keep talking with the client
while 1:
# Wait to accept a connection - blocking call
conn, addr = s.accept()
print('Connection initiated from ' + addr[0])
while 1:
buf = conn.recv(1024)
if len(buf) == 0:
print("Received buffer with no contents. Closing conn.")
conn.close()
print("Listening again")
break
else:
print("Received: "+str(buf))
conn.send(buf)
s.close()