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()
Stumbled upon this, OP might have solved this on his own but just in case. Here what I see, OP made several mistakes here.
print ('Awaiting command from client...')
and align their indentation to it. The code simply stuck in thewhile True:
loop and basically doing nothing meaningful except printing this two linesprint ('Got connection from', addr)
andprint ('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