0

I'm new to python and this time I want to send a file between two VMs, first of all, the VMs are configured with NAT Network, both VMs can ping each other. The codes are the following:

Server side:

#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((host, port))            # Bind to the port
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))

filename='runbonesi.py'
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()

client side:

#client.py
import socket                   # Import socket module

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

s.connect((host, port))

with open('received_file', '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')

The results can be seen in the following pictures:

Server side: result on server

Client side: result on client

The problem is, the client didn't receive the file as .py format, it only received as txt files. Please help me resolve this issue.

Thank you.

nugie
  • 23
  • 2
  • 5
  • What exactly the issue is? – Nestor Yanchuk Jan 15 '18 at 14:22
  • Welcome to Stack Overflow! When you created your account here, it was suggested you take the [tour](https://stackoverflow.com/tour) and read the [help center pages](https://stackoverflow.com/help) in order to familiarize yourself with the site. Specific pages that you should read include [How Do I Ask a Good Question?](https://stackoverflow.com/help/how-to-ask) and [How to Create a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). Please take these introductory steps and then edit your post accordingly. – chb Jan 15 '18 at 14:27
  • A first thing I notice is that the sever after establishing the connection with the client it tries to read "something" from the client `print 'Got connection from', addr` `data = conn.recv(1024)`, but the client it's not sending anything, it just starts to read too. So you either comment "the read" on the server or "send" something from the client. – Lohmar ASHAR Jan 15 '18 at 14:45
  • @NestorYanchuk I'm sorry, I edited the questions. The client didn't received the exact "runbonesi.py", it only received a txt file, the content is the same though.. – nugie Jan 16 '18 at 15:24

1 Answers1

0

After s.connect((host, port)) in the client script you have to write s.send(...) with 'hello', for example. You also forgot to write the file extension '.py' in with open(...) (only if you want to save the file as Python script!). And you also have to add s.send(...) before you close the connection on the client side.