2014-03-26 165 views
0

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

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下的文件名。例如,第一个客户将出局1,第7个出局7。提前致谢。

P.S.我是python的新手,所以如果你能向我解释你改变了它会很好,不要问我使用Twisted cause Id喜欢用普通的python套接字来做这件事。

+1

看看选择模块。 – Broseph

+0

['select'](http://devdocs.io/python/library/select)库就是为了这个目的。或者你可以使用Twisted等第三方库。 – hjpotter92

+0

嗯,你能给我写一个使用选择库的例子吗?我不喜欢使用第三部分库,所以如果你给我和例子,我会很高兴。我正在使用Windows 7,并且看到我的操作系统出现了一些问题,请选择,这是一个问题吗? – Maxpnl

回答

0

我有这个问题我自己:-)

所以你想要做的是有多个连接,但是通常插座使用主线程,因此很难有一个以上的连接。

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