Python sockets disconnect client

Asked

Viewed 898 times

1

I would like to make with a command of the server, was disconnected a client specific.

from socket import *



meuHost = ''


minhaPort = 50007


sockobj = socket(AF_INET, SOCK_STREAM)


sockobj.bind((meuHost, minhaPort))


sockobj.listen(5)


while True:
    conexão, endereço = sockobj.accept()                                       




    print('Server conectado por', endereço)

    while True:

        data = conexão.recv(1024)



        if not data: break

        conexão.send(b'Eco=>' + data)

If there is this attribute "Accept", there is some way to get "Kickar" or disconnect a client from my server?

  • Python3 accepts variables with accent -- but that doesn’t mean you should use them ....

1 answer

1


Have you ever just tried conexão.close()?

In fact the documentation says that to close immediately you can also use the method shutdown before the close:

conexão.shutdown()
conexão.close()
  • But in the.close connection argument () to insert a specific ip number of a user to be able to disconnect only that user?

  • 1

    Its connection variable, which is assigned on the line conexão, endereço = sockobj.accept() refers to a single connection (therefore, from a single user). This program of yours does not "meet" simultaneous connections in the socket. A new user will only be answered by this code when the while most internal end and that line runs again. To handle simultaneous connections, you must have a program that has a multi-threading, multi-processing, or asynchronous strategy.

  • 1

    Direct programming on sockwts done the right way is not an easy thing. It is important to understand how it works, learn, etc... but for production, it is better to use a higher level protocol, with a server of some framework.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.