2016-04-22 18 views
0

クライアントからサーバーにファイルを転送するために使用するサーバー/クライアントソケットプログラムがあります。問題は、ファイルが転送されるとコードの実行が停止することです。サーバーサイドのソケットコードを連続して実行する方法

import socket 


host = '' 
port = 5560 

def setupServer(): 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    print("Socket created.") 
    try: 
     s.bind((host, port)) 
    except socket.error as msg: 
     print(msg) 
    print("Socket bind comlete.") 
    return s 

def setupConnection(): 
    s.listen(1) # Allows one connection at a time. 
    conn, address = s.accept() 
    print("Connected to: " + address[0] + ":" + str(address[1])) 
    return conn 

def storeFile(filePath): 
    picFile = open(filePath, 'wb') 
    print(filePath) 
    print("Opened the file.") 
    pic = conn.recv(1024) 
    #print(pic) 
    while pic: 
     print("Receiving picture still.") 
     picFile.write(pic) 
     pic = conn.recv(1024) 
    picFile.close() 

def dataTransfer(conn): 
    # A big loop that sends/receives data until told not to. 
    while True: 
     # Receive the data 
     data = conn.recv(1024) # receive the data 
     data = data.decode('utf-8') 
     # Split the data such that you separate the command 
     # from the rest of the data. 
     dataMessage = data.split(' ', 1) 
     command = dataMessage[0] 
     if command == 'GET': 
      reply = GET() 
     elif command == 'REPEAT': 
      reply = REPEAT(dataMessage) 
     elif command == 'STORE': 
      print("Store command received. Time to save a picture") 
      storeFile(dataMessage[1]) 
      reply = "File stored." 
     elif command == 'LED_ON': 
      callLED() 
      reply = 'LED was on' 
     else: 
      reply = 'Unknown Command' 
     # Send the reply back to the client 
     conn.sendall(str.encode(reply)) 
     #print("Data has been sent!") 
    conn.close() 


s = setupServer() 

while True: 
    try: 
     conn = setupConnection() 
     dataTransfer(conn) 
    except: 
     break 

クライアント側のコード:私はそれ

私は再びコードを実行することなく、ファイルを複数回転送することができるように、サーバー側のコードが連続して実行されていることなどして、再度サーバーのコード変更したいです次のとおりです。

import socket 
from time import sleep 
from time import time 


host = '192.168.0.17' 
port = 5560 

data = "hi" 
filepath = "/var/www/html/unknown.txt" 
def setupSocket(): 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.connect((host, port)) 
    return s 

def sendPic(s, filePath): 
    print(filePath) 
    pic = open(filePath, 'rb') 
    chunk = pic.read(1024) 
    s.send(str.encode("STORE " + filePath)) 
    t = time() 
    while chunk: 
     print("Sending Picture") 
     s.send(chunk) 
     #print(chunk) 
     chunk = pic.read(1024) 
    pic.close() 
    print("Done sending") 
    print("Elapsed time = " + str(time() - t) + 's') 
    #s.close() 
    return "Done sending" 

def sendReceive(s, message): 
    s.send(str.encode(message)) 
    reply = s.recv(1024) 
    print("We have received a reply") 
    print("Send closing message.") 
    s.send(str.encode("EXIT")) 
    #s.close() 
    reply = reply.decode('utf-8') 
    return reply 

def transmit(message): 
    s = setupSocket() 
    response = sendReceive(s, message) 
    return response 

def backup(filePath): 
    s = setupSocket() 
    response = sendPic(s, filePath) 
    return response 

while True: 
    backup(filepath) 
    print("Backup Complete!") 
    break 

私はこのコードを所有していません。私はYouTubeのビデオから得たコードを少し変更しました。

答えて

0

SocketServerモジュールを見ましたか? dataTransfer()関数をRequestHandlerクラスのhandle()メソッドとしてセットアップし、次にserve_forever()メソッドを使用してサーバーを起動できます。

関連する問題