Article 56KW8 how to end looping socket server (python script)

how to end looping socket server (python script)

by
cmgeo
from LinuxQuestions.org on (#56KW8)
hello,

this is my partial server script:
Code:def proceessConnection(conn, addr):
global __continueProgamme
with conn:
#print('Connected by', addr)
while True and __continueProgamme == 1:
data = conn.recv(64)
print(type(data), data)
if not data:
break

if data == b'EXIT':
#print('settng to exit')
__continueProgamme = 0
exit(0)
break
elif len(data.decode('utf-8').split(".")) == 4:
conn.sendall(bytearray(whichCountry((data.decode('utf-8'))),'utf-8'))
else:
conn.sendall(data)
#print("Ending connection",addr)

def getSocket():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(type(s), s)
s.bind(('127.0.0.1', 50000))
return s
except Exception as e:
#print("Waiting for socket")
time.sleep(1)
return getSocket()

s=getSocket()

__continueProgamme = 1

while True:
#print("Ready to Accept")
if __continueProgamme != 1:
break

s.listen()
conn, addr = s.accept()
childT = threading.Thread(target=proceessConnection, args=(conn, addr))
childT.start()
#print('finished threading start')
if __continueProgamme != 1:
break
#print("not set to exit")and this is my client script
Code:
def sendToServer(msg):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 50000))
s.sendall(msg)
data = s.recv(1024)
print(type(data), data)
print('Received', repr(data))

sendToServer(b'Hello, world')
sendToServer(b'31.13.127.8')
sendToServer(b'EXIT')
sendToServer(b'EXIT')my problem is that i wish to stop the server on first exit but it does not and only does so on second attempt where it returns exception ConnectionResetError.

i want to smoothly exit the server on first exit.

how do i achieve that.

Thanks for looking into it.

CMGlatest?d=yIl2AUoC8zA latest?i=5Rd9Y-ldYY8:2gmkUMFqEtE:F7zBnMy latest?i=5Rd9Y-ldYY8:2gmkUMFqEtE:V_sGLiP latest?d=qj6IDK7rITs latest?i=5Rd9Y-ldYY8:2gmkUMFqEtE:gIN9vFw5Rd9Y-ldYY8
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments