Using sockets to access file from remote host (Python)

3.4k Views Asked by At

Pasted below is my client and server python scripts. TCP connection is well established. Server is listening well. For time being, I have made my computer client as well as server both. Basically, I am not receiving the file. The client side just says receiving the file and then nothing more than that. Server is alos just listening. Not acknowledging anything. Have a look at my way of opening files and guide me through this.

SERVER.py
import socket
import sys
import os.path
import operator

serverPort = 5005
#create socket object for server
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('192.168.1.8',serverPort))     
serverSocket.listen(1) 

print ('Server listening...')
print (socket.gethostbyname(socket.gethostname()))

while True:

    #return value of accept() is assigned to socket object
    #and address bound to that socket
    connectionSocket, addr = serverSocket.accept()

    #connection established with client
    print ('Got connection from', addr)
    print ('Awaiting command from client...')

client_request = connectionSocket.recv(1024)        
file_name = client_request.recv(1024)

f = open(file_name, "rb")
print('Sending file...')
l = f.read(1024)
while(l):
        connectionSocket.send(l)
        l = f.read(1024)
        f.close()
print('Done sending')
connectionSocket.close()

while the client script is below:

import socket
import sys
serverName = '192.168.1.8'
serverPort = 49675

#in this loop, sockets open and close for each request the client makes
#while True:
 #create socket object for client
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
print('Connected to server.')
fileName = "C:\\Users\\dell\\Desktop\\mano.txt"
clientSocket.send(fileName)
    #if sentence == 'GET':
f = open(fileName, "wb")
print('Receiving file..')
l = clientSocket.recv(1024)
while (l):
    f.write(l)
    l = clientSocket.recv(1024)
    f.close()
print('Done receiving file')
clientSocket.close()
1

There are 1 best solutions below

0
On

Stumbled upon this, OP might have solved this on his own but just in case. Here what I see, OP made several mistakes here.

  1. As mentioned by Stevo the port number used in Sever and Client should be the same port number, in this case either 5005 or 49675 would work. Any other port number that's above 1024 is fine actually as OS usually restricts usage of port numbers lower than that. https://hub.packtpub.com/understanding-network-port-numbers-tcp-udp-and-icmp-on-an-operating-system/ you can read about port numbers and stuff in this link.
  2. Your Server file itself have coding mistakes. You first have to indent all lines after print ('Awaiting command from client...') and align their indentation to it. The code simply stuck in the while True: loop and basically doing nothing meaningful except printing this two lines print ('Got connection from', addr) and print ('Awaiting command from client...'). Moreover, your client_request is a byte object, byte object does not contain method of recv method.

Upon changing those small mistakes made in the Server side, I am able to run it and receive messages from Client. This is what it looks like. Perhaps this page maybe of help: https://docs.python.org/3/library/socket.html