I wrote the code for a simple TCP client:
from socket import *
# Configurações de conexão do servidor
# O nome do servidor pode ser o endereço de
# IP ou o domínio (ola.python.net)
serverHost = 'localhost'#ip do servidor
serverPort = 50008
# Mensagem a ser mandada codificada em bytes
menssagem = [b'Ola mundo da internet!']
# Criamos o socket e o conectamos ao servidor
sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.connect((serverHost, serverPort))
# Mandamos a menssagem linha por linha
for linha in menssagem:
sockobj.send(linha)
# Depois de mandar uma linha esperamos uma resposta
# do servidor
data = sockobj.recv(1024)
print('Cliente recebeu:', data)
# Fechamos a conexão
sockobj.close()
I would like to know, how " generate " multiple clients TCP using Threads instead of opening multiple instances of the terminal and run the script several times.
threadingPython module might help you. Or trymultiprocessingif you wanna break out of the GIL (Global Interpreter Lock - better Google that term). If you're using Python 3.5, you should almost definitely tryasyncioand that fancyasync defsyntax for asynchronous functions definition.threading.Thread(target=func).start())threading: docs.python.org/3.4/library/threading.html