簡體   English   中英

嘗試通過FTP在客戶端中將字符串從客戶端發送到服務器時傳遞'b'

[英]'b' is getting passed when sending string from client to server in python while trying FTP implementation

我試圖在我想從客戶端向服務器發送文件名的地方實現FTP,我嘗試了以下代碼,當我給文件名指定為myText.txt但服務器接收為“ b” myText.txt”時

您能幫我擺脫B嗎? 這是服務器上的輸出:

這是服務器代碼:

import socket                   # Import socket module
port = 60000                    # Reserve a port for your service.
socketObj = socket.socket()     #Create a socket object
host = socket.gethostname()     # Get local machine name
socketObj.bind((host, port))    # Bind to the port
socketObj.listen(5)             # Now wait for client connectionection.
print ('Server listening....')

while True:
    connection, addr = socketObj.accept()     # Establish connectionection with client.
    print ('Got connectionection from', addr)
    data = connection.recv(1024)
    print('Server received request for FTS of',(data))

    filename=(repr(data))
    f = open(filename,'rb')
    l = f.read(1024)
    while (l):
       connection.send(l)
       print('Sent ',repr(l))
       l = f.read(1024)
    f.close()

    print('Done sending')
    connection.send(('Thank you for connectionecting').encode())
    connection.close()

這是客戶端代碼

import socket                   # Import socket module

s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
port = 60000                    # Reserve a port for your service.

s.connect((host, port))
fileNeeded = input("What File do you need, please enter the name:")
s.send(fileNeeded.encode())

fileToBeSaved = input("Enter file name to save requested file")

with open(fileToBeSaved, 'wb') as f:
    print ('file opened')
    while True:
        print('receiving data...')
        data = s.recv(1024)
        print((data))
        if not data:
            break
        # write data to a file
        f.write(data)

f.close()
print('Successfully got the file')
s.close()
print('connection closed')

服務器中收到以下消息:服務器收到了對b'mytext.txt'的FTS的請求

您可以使用bytes.decode()方法將字節轉換為字符串:

更改:

filename=(repr(data))

至:

filename=repr(data).decode()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM