繁体   English   中英

Python套接字处理多重连接

[英]Python Socket Handling Multiple Connection

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

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下的文件名对于每个客户端都将更改。 例如,第一个客户端将退出,第一个客户端将退出。 提前致谢。

附注:我是python的新手,所以如果您能向我解释您所做的更改,那将是很棒的事情,请不要让我使用Twisted原因ID,我想使用普通的python套接字来做到这一点。

我自己有这个问题:-)

因此,您尝试执行的操作是具有多个连接,但是套接字通常使用主线程,因此很难拥有多个连接。

为了解决这个问题,我们需要使用一种叫做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))

希望这可以帮助! :-)

请告诉我是否可以,因为我还没有测试过,所以我才做过:)

谢谢,

〜酷

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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