2017-11-28 2 views
0

원격 우분투 서버로 파일을 보낼 수있는 파일을 만들고 있습니다. 그러나 이름을 보낼 때 인쇄물을 넣으면 "b"가됩니다 " 어떻게 파일을 열 수 있습니까? 사전에파이썬에서 소켓으로 파일 이름 지정하기

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. :
  3. 봅니다

    (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()) 
    
  4. server.py을

  5. 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() 
    
  6. 당신에게

  7. 감사
+0

고마워요. 이걸 생각하고 있었지만, 알지 못했습니다. 고마워요. –

관련 문제