2017-11-28 7 views
0

リモートのUbuntuサーバーにファイルを送信できるファイルを作成しています。 しかし、私はそれの名前を送信すると、私は印刷物を入れ、それはちょうど "b"である ファイルを開くことができますか?Pythonでソケットにファイルを命名

Client.py

import socket 
with socket.socket() as s: 
    s.connect(('139.59.173.187',8000)) 
    with open('User.txt','rb') as f: 
     s.sendall(f.read()) 
    name = input("What would you like it to be saved as?") 
    s.send(name) 

Server.py

import socket 
with socket.socket() as s: 
    s.connect(('139.59.173.187',8000)) 
    with open('User.txt','rb') as f: 
     s.sendall(f.read()) 
    name = input("What would you like it to be saved as?") 
    s.send(name) 

事前に感謝

`

+0

_server.py_と_client.py_の両方のファイルは同じです。 – javaPlease42

答えて

0
  1. あなたserver.pyとclient.py両方のファイルは同じです。私はあなたが間違ってそれらを複製したと思います。問題について
  2. てみ

    (python2.7中)(のpython3で)リモートサーバに最初

    import socket 
    
    with socket.socket() as s: 
        s.connect(('127.0.0.1',22599)) 
    
        name = input("What would you like it to be saved as?") 
    
        s.send(name.encode('utf-8')) 
    
        with open('xyz.txt','rb') as f: 
         s.sendall(f.read()) 
    
  3. server.pyを

  4. clients.pyを名前を送信します

    import socket # Import socket module 
    s = socket.socket()   
    host = '127.0.0.1' #Ge Local Machine 
    port = 22599    
    s.bind((host, port)) # Bind to the port 
    s.listen(3) #wait for client to join 
    while True: 
        c, addr = s.accept()  
        print 'Connection Accepted From',addr 
        print "File is comming ...." 
        file = c.recv(1024)  #get file name first from client 
        #opening file first 
    
        f = open(file,'wb')  #open that file or create one 
        l = c.recv(1024)   #get input 
        while (l): 
    
         print "Receiving...File Data" 
         f.write(l)   #save input to file 
         l = c.recv(1024)  #get again until done 
    
        f.close() 
    
    c.send('Thank you for connecting') 
    c.close() 
    
  5. ありがとうございました
+0

これをありがとう、私はこれを考えていたが、didntは知っていた。どうもありがとう –