简体   繁体   English

尝试通过FTP在客户端中将字符串从客户端发送到服务器时传递'b'

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

I am trying to implement to implement FTP where I want to send Filename to server from client, I have tried below code, when I give file name as myText.txt but server is receiving as 'b"myText.txt'" 我试图在我想从客户端向服务器发送文件名的地方实现FTP,我尝试了以下代码,当我给文件名指定为myText.txt但服务器接收为“ b” myText.txt”时

Can you please help me how can I get rid of b ? 您能帮我摆脱B吗? This is the output on server: 这是服务器上的输出:

This is server code: 这是服务器代码:

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()

This is the client code 这是客户端代码

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')

The following is received in server: Server received request for FTS of b'mytext.txt' 服务器中收到以下消息:服务器收到了对b'mytext.txt'的FTS的请求

You can use the bytes.decode() method to convert bytes into a string: 您可以使用bytes.decode()方法将字节转换为字符串:

Change: 更改:

filename=(repr(data))

to: 至:

filename=repr(data).decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM