Python Socket Programming, other programs aren't connecting to the server


I am opening the same program multiple times (10 to be exact) and i they are all trying to solve the random number first. When they solve this number they open a server which all of the other same programs are listening to on separate threads waiting to connect and when they do finally connect i want them all to shutdown.

import random
import os
import socket
import threading
import time

host = '192.168.1.139'
port = 8011

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def verified():
    print('Connection made successfuly')
    time.sleep(10)
    quit()

def network():
    try:
        s.connect(('192.168.1.135',8014))
        f = s.recv(50)
        finished = f.decode('utf-8')
        if finished == 'completed':
            quit()

    except:
        pass

def winner():
    x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    x.bind(('192.168.1.135', 8014))
    x.listen(10)
    con, address = x.accept()
    con.send(bytes('completed', 'utf-8'))

def mine():
    global guess
    time.sleep(.000001)
    guess = random.randint(0,1000)
    print(guess)
    tran = 500



while True: 
    mine()
    if guess == 500:
        print("Solved")
        winner()
        break;
    else:
        x = threading.Thread(target=network)
        x.start()

For some reason it is connecting to the server but it isn't quitting the program, could this be because it is connecting to itself and exiting to fast? Please help, Thank You!



Actually, I was consfused, but apparently when the quit() is in a function it can't end the whole process, so what I would suggest you to do is to make the while loop with a variable that network would be able to change (or even use guess):

guessing = True
while guessing:
    mine()
    if guess == 500:
        print("Solved")
        winner()
        break;
    else:
        x = threading.Thread(target=network)
        x.start()

and change the network to:

def network():
    try:
        s.connect(('192.168.1.135',8014))
        f = s.recv(50)
        finished = f.decode('utf-8')
        if finished == 'completed':
            #quit()
            global guessing
            guessing = False

    except:
        pass

I saw that the two constants host and port were not used


Unfortunately the winner function is just accepting one answer (she's a little introvert) but we can solve that problem:

def winner():
    x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    x.bind(('192.168.1.135', 8014))
    x.listen(10)
    connected = 0
    while connected < 10:
        con, address = x.accept()
        con.send(bytes('completed', 'utf-8'))
        connected += 1

and yay, she is making friends


ok, but now she is in the outer world where she needs to talk to a lot of people not just limited 10, so she goes to the bus stop (she's going home), she waits a little bit to see if someone will talk to her (for any reason), no one said anything for some time, so, she puts her airpods and starts listening to some good {insert here a music that a computer function likes to listen to} So, to implement this:

def winner():
    x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    x.settimeout(5) #  WAITS FOR FIVE SECONDS
    x.bind(('192.168.1.135', 8014))
    x.listen()
    while True:
        try:
            con, address = x.accept()
            con.send(bytes('completed', 'utf-8'))
        except socket.timeout: # IF IN FIVE SECONDS, RECEIVES NO CONNECTION
            break

the x.settimeout(5) will make the connections give an socket.timeout error if the time set (in case 5) is exceeded without any response of the other side.
now I remembered that using threading we could push clients to another function and "free" the queue of the clients, but I think that for this special case this solution is better