This is my Python script to receive data from client:
import time
import socket
HOST = ''
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
try:
s.bind((HOST, PORT))
except socket.error as socketError:
print('socket binding failed, ', socketError)
print('Exiting...')
sys.exit(0)
print('Socket binding complete')
s.listen(1)
print('Socket listening for connection...')
conn, addr = s.accept()
print('connected to ', addr[0])
bfr = b''
try:
while True:
while True:
temp = conn.recv(1024)
print('temp is ',temp.decode('utf-8'))
print('buffer value ', bfr.decode('utf8'))
if not temp:
break
bfr += temp;
data = bfr.decode('utf-8')
bfr = b''
print('value received,', data)
if data == 'Connection-Ready to receive commands':
print('')
#other conditions
except Exception as loopException:
print("Exception occurred in loop, exiting...")
finally:
s.close()
The script hangs up after printing
connected to 192.168.4.197
and is not accepting any commands from client. if the client disconnects, all the send commands are printed.
Why is it behaving like this?
Update 1
Tried removing the inner while and added a time.sleep(.10)
and the script is not hanging up but only the first command is printing as is and the rest is printing with a linebreak after the first character like, if I send 10, it will print 1 first and 0 in another line. All I want is to get the commands as it is sent from the client, every command is single word.