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