Windows10下VB5.2.4中Ubuntu16.04虚拟机间Python传文件格式异常求助
Hey there! I see you're new to Python and running into a small hiccup with your socket file transfer—your client is saving the file as a .txt instead of keeping the original .py extension. Let's break this down and fix it quickly.
The Root Cause
The main issue here is in your client code: you're saving the file with the name received_file which has no file extension. Your operating system is just defaulting to labeling it as a text file (.txt) because it doesn't see a specific extension associated with it.
Quick Fix: Add the .py Extension Directly
The simplest fix is to just add the .py extension when you open the file in client.py:
with open('received_file.py', 'wb') as f: print 'file opened' while True: print('receiving data...') data = s.recv(1024) print('data=%s', (data)) if not data: break # write data to a file f.write(data)
Once you make this change, the saved file will have the correct .py extension instead of being defaulted to .txt.
More Flexible Approach: Send Filename from Server to Client
If you want to transfer different types of files without hardcoding extensions every time, you can modify your code to send the filename from the server to the client first. Here's how to adjust both scripts:
Updated Server Code (server.py)
#server.py import socket # Import socket module port = 60000 # Reserve a port for your service. s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name s.bind(('', port)) s.listen(5) # Now wait for client connection. print 'Server listening....' while True: conn, addr = s.accept() # Establish connection with client. print 'Got connection from', addr data = conn.recv(1024) print('Server received', repr(data)) # Send the filename to the client first filename='runbonesi.py' conn.send(filename) # Send the filename string # Now send the file content f = open(filename,'rb') l = f.read(1024) while (l): conn.send(l) print('Sent ',repr(l)) l = f.read(1024) f.close() print('Done sending') conn.shutdown(socket.SHUT_WR) print conn.recv(1024) conn.close()
Updated Client Code (client.py)
#client.py import socket # Import socket module s = socket.socket() # Create a socket object host = '10.0.2.15' port = 60000 # Reserve a port for your service. s.connect((host, port)) # First receive the filename from the server filename = s.recv(1024) print(f"Receiving file: {filename}") # Save the file using the received filename with open(filename, 'wb') as f: print 'file opened' while True: print('receiving data...') data = s.recv(1024) print('data=%s', (data)) if not data: break # write data to a file f.write(data) f.close() print('Successfully get the file') s.close() print('connection closed')
This way, no matter what file you send from the server (.py, .txt, .jpg, etc.), the client will save it with the exact same filename and extension as the original.
Quick Side Notes
- Your code uses Python 2 syntax (like
print 'Server listening....'without parentheses). Since Ubuntu 16.04 comes with Python 2 pre-installed, this works fine, but if you ever upgrade to Python 3, you'll need to update those print statements to useprint(...). - Double-check that your file transfer is complete—your current loop logic for reading/sending chunks looks solid, but if you ever notice truncated files, you could add a step to send the file size first, so the client knows exactly how much data to expect.
That should resolve your file extension problem! Feel free to ask if you hit any other issues with your socket setup.
内容的提问来源于stack exchange,提问作者nugie

