在尝试FTP实现时,在python中将字符串从客户端发送到服务器时,传递了“b”
我正在尝试实现FTP,在这里我想从客户端向服务器发送文件名,我尝试了下面的代码,当我将文件名命名为myText.txt,但服务器接收到的文件名为“bmyText.txt” 你能帮我摆脱b吗? 这是服务器上的输出: 这是服务器代码:在尝试FTP实现时,在python中将字符串从客户端发送到服务器时,传递了“b”,python,python-3.x,sockets,ftp,Python,Python 3.x,Sockets,Ftp,我正在尝试实现FTP,在这里我想从客户端向服务器发送文件名,我尝试了下面的代码,当我将文件名命名为myText.txt,但服务器接收到的文件名为“bmyText.txt” 你能帮我摆脱b吗? 这是服务器上的输出: 这是服务器代码: import socket # Import socket module port = 60000 # Reserve a port for your service. socketObj =
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请求。您可以使用该方法将字节转换为字符串:
更改:
filename=(repr(data))
致:
如果你去掉reprdata,你就会解决这个问题。
filename=repr(data).decode()