简体   繁体   中英

TCP threaded python

I'm learning some Networking through Python and came up with this idea of TCPServer Multithread so i can have multiple clients connected. The problem is that i can only connect one client.

import socket
import os
import threading
from time import sleep
from threading import Thread

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Preparado...')

def Main():
host = '127.0.0.1'
port = 5000

s.bind((host, port))
print('Enlaze listo...')

print('Escuchando...')
s.listen(5)
c, addr, = s.accept()
os.system('cls')

print('Conexion desde: '+str(addr))

def handle_client(client_socket):
while True:
    data = client_socket.recv(1024).decode('utf-8')
    if not data: break
    print('Client says: ' + data)
    print('Sending: ' + data)
    client_socket.send(data.encode('utf-8'))
client_socket.close()

if __name__ == '__main__':
while True:
    Main()
    client_socket, addr = s.accept()
    os.system('cls')
    print('Conexion desde: '+str(addr))
    Thread.start_new_thread(handle_client ,(client_socket,))
s.close()

Edit: This is my actual code, to test it i open up two Client.py codes and try to connect to it. The first Client.py successfully connects (Although there's bugs in receiving and sending back info)The second one executes but it's not shown in the server output as connected or something, it just compiles and stays like that.

You need to create a new thread each time you get a new connection

import socket
import thread

host = '127.0.0.1'
port = 5000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Ready...')

s.bind((host, port))
print('Bind Ready...')

print('Listening...')
s.listen(1)

def handle_client(client_socket):
    while True:
        data = client_socket.recv(1024)
        if not data: break
        print('Client says: ' + data)
        print('Sending: ' + data)
        client_socket.send(data)
    client_socket.close()

while True:
    client_socket, addr = s.accept()
    print('Conexion from: '+str(addr))
    thread.start_new_thread(handle_client ,(client_socket,))
s.close()

Ok, here's the code solved, i should have said i was working on Python3 version. Reading the docs i found out, here's the code and below the docs.

import socket
import os
import threading
from time import sleep
from threading import Thread
import _thread

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket Preparado...')

def Main():
host = '127.0.0.1'
port = 5000

s.bind((host, port))
print('Enlaze listo...')

print('Escuchando...')
s.listen(1)





def handle_client(client_socket):
while True:
    data = client_socket.recv(1024).decode('utf-8')
    if not data: break
    print('Client says: ' + data)
    print('Sending: ' + data)
    client_socket.send(data.encode('utf-8'))
client_socket.close()

if __name__ == '__main__':
Main()
while True:

    client_socket, addr = s.accept()
    os.system('cls')
    print('Conexion desde: '+str(addr))           
    _thread.start_new_thread(handle_client ,(client_socket,))
s.close()

https://docs.python.org/3/library/threading.html http://www.tutorialspoint.com/python3/python_multithreading.htm

The problem was at _thread.start_new_thread(handle_client ,(client_socket,)) just import _thread ask some questions here, keep researching and got it.

Thanks all of you.

WhiteGlove

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM