Added broken pipe handling.
This commit is contained in:
parent
3a7894e052
commit
df59ddfc34
58
slave.py
58
slave.py
@ -1,7 +1,8 @@
|
||||
import socket
|
||||
import sys
|
||||
|
||||
ledNames = [
|
||||
import socket
|
||||
import sys
|
||||
|
||||
|
||||
ledNames = [
|
||||
"power",
|
||||
"lan0",
|
||||
"lan1",
|
||||
@ -16,8 +17,8 @@ import sys
|
||||
"user2"
|
||||
]
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, file=sys.stderr, **kwargs)
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, file=sys.stderr, **kwargs)
|
||||
|
||||
def setLed(index, color):
|
||||
try:
|
||||
@ -37,7 +38,8 @@ def setLed(index, color):
|
||||
finally:
|
||||
file.close()
|
||||
|
||||
def handleData(data):
|
||||
|
||||
def handleData(data):
|
||||
databyline = data.split("\n")
|
||||
try:
|
||||
for i in range(len(databyline)):
|
||||
@ -52,29 +54,29 @@ def setLed(index, color):
|
||||
def setAutonomous(data):
|
||||
pass
|
||||
|
||||
# Create a TCP/IP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Bind the socket to the port
|
||||
server_address = ('0.0.0.0', 10000)
|
||||
# Create a TCP/IP socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
# Bind the socket to the port
|
||||
server_address = ('0.0.0.0', 10000)
|
||||
try:
|
||||
sock.bind(server_address)
|
||||
sock.bind(server_address)
|
||||
except OSError:
|
||||
server_address = ('0.0.0.0', 10001)
|
||||
sock.bind(server_address)
|
||||
|
||||
eprint("Server started on {} on port {}".format(server_address[0], server_address[1]))
|
||||
eprint("Server started on {} on port {}".format(server_address[0], server_address[1]))
|
||||
|
||||
|
||||
# Listen for incoming connections
|
||||
sock.listen(1)
|
||||
|
||||
while True:
|
||||
# Wait for a connection
|
||||
eprint('waiting for connection')
|
||||
conn, client_address = sock.accept()
|
||||
try:
|
||||
eprint('connection from {}'.format(client_address))
|
||||
# Listen for incoming connections
|
||||
sock.listen(1)
|
||||
|
||||
while True:
|
||||
# Wait for a connection
|
||||
eprint('waiting for connection')
|
||||
conn, client_address = sock.accept()
|
||||
try:
|
||||
eprint('connection from {}'.format(client_address))
|
||||
while True:
|
||||
allData = "";
|
||||
while True:
|
||||
@ -84,13 +86,15 @@ while True:
|
||||
allData+=data.decode('utf-8')
|
||||
if allData.find('\n\n') > -1:
|
||||
break
|
||||
eprint('all data: {}'.format(allData))\
|
||||
eprint('all data: {}'.format(allData))
|
||||
|
||||
if not handleData(allData) is 0:
|
||||
conn.sendall("error".encode('utf-8'))
|
||||
else:
|
||||
conn.sendall("OK".encode('utf-8'))
|
||||
|
||||
finally:
|
||||
# Clean up the connection
|
||||
except BrokenPipeError:
|
||||
eprint("connection from {} has broken a pipe.".format(client_address))
|
||||
conn.close()
|
||||
finally:
|
||||
# Clean up the connection
|
||||
conn.close()
|
||||
|
Loading…
Reference in New Issue
Block a user