2016-05-15 21 views
0

私は非常に基本的な問題があります。クライアント側はファイルを完全に受信して接続を終了しています。サーバー側は、ファイルとこのread(1024)行を閉じることに問題があります。たぶん私は間違った位置に置いています。私が思うwhileループと混同しています。これで私を導いてください。シンプルなコードは、ファイルを閉じているし、それから読み取るしようとして次のループサイクルでは、このようにエラーI/O operation on closed fileを取得し、あなたのwhileループでは閉じたファイルに対する入出力操作(インデントの問題)Python-ソケット

while True: 
    conn, addr = s.accept()  # Establish connection with client. 
    print 'Got connection from', addr 
    input = raw_input("Enter 1 for accessing file and 2 for editting") 

    print(repr(input)) # printing input 
    if (input == "1"): 
     filename= conn.recv(1024) 
     f = open(filename,'rb') 
     l = f.read(1024) 

    while (1): #Keep sending it until EOF id found 
     conn.send(l) #Keep sending the opened file 
     print(repr(l)) 
     l = f.read(1024) 
     f.close() 

答えて

0

の下に貼り付けられます。誤って配置されたf.close()以外にも、ループにブレーク条件がありません。より良い

for l in iter(lambda: f.read(1024), ''): #Keep sending it until EOF id found 
     conn.send(l)       #Keep sending the opened file 
    conn.close() 
    f.close() 

 l = f.read(1024) 

    while (1): #Keep sending it until EOF id found 
     conn.send(l) #Keep sending the opened file 
     print(repr(l)) 
     l = f.read(1024) 
     f.close() 

を置き換えます

関連する問題