2014-03-26 2 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 아래의 파일 이름이 모든 클라이언트에 대해 변경됩니다. 예를 들어 첫 번째 클라이언트는 out1이되고 7 번째 클라이언트는 out7이됩니다. 미리 감사드립니다.

P. 나는 파이썬에 익숙하지 않다. 그래서 내가 변경 한 것을 잘 설명해 준다면, 일반적인 파이썬 소켓으로 이것을하기 위해 Twisted cause ID를 사용하라고하지 말아라.

+1

선택 모듈을보십시오. – Broseph

+0

['select'] (http://devdocs.io/python/library/select) 라이브러리는이를위한 것입니다. 또는 Twisted와 같은 타사 라이브러리를 사용할 수 있습니다. – hjpotter92

+0

음, 선택 라이브러리를 사용하는 예를 써주시겠습니까? 세 번째 파트 라이브러리를 사용하는 것을 좋아하지 않으므로 나와 함께 제공하면 기쁠 것입니다. 나는 창 7에 종사하고 나는 나의 operative 체계 및 선택에 대한 약간 문제가다는 것을 보았다, 이것은 문제인가? – Maxpnl

답변

0

내가

:-)이 문제를 자신했다 그래서 당신이하려는 것은 그러나 일반적으로 소켓은 하드 하나 개 이상의 연결이 만들고, 메인 스레드를 사용하여 여러 개의 연결을 가지고 있습니다했습니다.

이 문제를 해결하려면 스레딩이라는 항목을 사용해야합니다.이를 통해 작업을 다른 스레드로 넘길 수 있습니다. 따라서 모든 연결이 이루어질 때마다 새 스레드를 만들어야합니다.

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

:)

감사 전에 그것을 한 적이