Python套接字处理多个连接

时间:2014-03-26 18:09:43

标签: python

我想让多个客户端连接到我的服务器,让服务器向他们发送不同的项目。例如,向第一个客户端发送“hi”,向第二个客户端发送“goodbye”。这是我的代码:

Server

import socket
file_num = 0
inp = raw_input("Name of the wordlist file = ")
inp2 = input("Number of lines for every wordlist = ")
with open(inp) as in_file:
    for line_num, line in enumerate(in_file):
        print line_num
        if not line_num % inp2:
            file_num += 1
        with open("out{0}.txt".format(file_num), "a") as out_file:
            out_file.writelines(line)
def upload(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(5)
    filename = open("out1.txt", "rb")
    print "Server Waiting for client on port ", port
    while 1:
        client_socket, address = server_socket.accept()
        print "Connection from ", address
        while 1:
            for line in filename:
                server_data = line
                if server_data.lower() == 'q':
                    client_socket.send(server_data)
                    client_socket.close()
                    break
                else:
                    client_socket.send(server_data)

                client_data = client_socket.recv(1024)
                if client_data.lower() == 'q':
                    print "Quit from client"
                    client_socket.close()
                    break
                else:
                    print "<-- client: ", client_data
        break
upload("localhost", 4000)

然后我的客户端程序

Client

import socket

port = 4000
host_server = "localhost"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host_server, port))
z = 1
print "Type 'Q' or 'q' to QUIT"
f = open("pino.txt", "w")
while 1:
    server_data = client_socket.recv(1024)
    f.writelines(server_data)
    if server_data.lower() == 'q':
        print "Quit from server"
        client_socket.close()
        break
    else:
        print "<-- server: ", server_data
        client_data = ("Sent "+str(z))
        z = z+1
        if client_data.lower() != 'q':
            client_socket.send(client_data)
        else:
            client_socket.send(client_data)
            client_socket.close()
            break
f.close()

希望你给我一个解决方案,因为如果它有效,这将是很酷的,我希望这个程序的另一件事是def upload下的文件名对每个客户都会改变。例如,第一个客户端将获得out1而第7个客户端将获得out7。提前致谢。

P.S。我是python的新手,所以如果你能解释一下你改变了什么就会很棒,不要让我使用Twisted导致我喜欢用普通的python套接字执行此操作。

1 个答案:

答案 0 :(得分:0)

我自己也遇到过这个问题: - )

所以你要做的就是有多个连接,但是通常socket使用主线程,这使得很难有多个连接。

要解决此问题,我们需要使用名为 Threading 的东西,它允许您超越其他线程的操作。因此,您需要在建立每个连接时创建一个新线程:

import socket
from _thread import *

file_num = 0
inp = raw_input("Name of the wordlist file = ")
inp2 = input("Number of lines for every wordlist = ")
with open(inp) as in_file:
    for line_num, line in enumerate(in_file):
        print line_num
        if not line_num % inp2:
            file_num += 1
        with open("out{0}.txt".format(file_num), "a") as out_file:
            out_file.writelines(line)
def upload(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(5)
    filename = open("out1.txt", "rb")
    print "Server Waiting for client on port ", port
    while 1:
        client_socket, address = server_socket.accept()
        print "Connection from ", address
        while 1:
            for line in filename:
                server_data = line
                if server_data.lower() == 'q':
                    client_socket.send(server_data)
                    client_socket.close()
                    break
                else:
                    client_socket.send(server_data)

                client_data = client_socket.recv(1024)
                if client_data.lower() == 'q':
                    print "Quit from client"
                    client_socket.close()
                    break
                else:
                    print "<-- client: ", client_data
        break
start_new_thread(upload, ("localhost", 4000))
#NOTICE!!! If this line doesn't work ^^^
#Then replace it with:
#thread.start_new_thread(upload, ("localhost", 4000))

希望这有帮助! : - )

如果不是,请告诉我, 因为我没有对此进行了测试,我之前刚刚完成过它:)

谢谢,

〜Coolq