I am new to python. Using Ubuntu 18.04, python 3.6.
Tried writing a script to send ANY File from server to client (currently trying to send .pdf file) and vice versa.
Earlier i used sockets to send file, but was facing problems with extensions (like sending .pdf file but received .txt file - could not figure out why this was happening). Then used ftp, but stuck at one point right now.
SERVER SCRIPT:
import socket
import os
s = socket.socket()
host = ""
port = 9999
s.bind((host, port))
s.listen(5)
print("Binding Done\n")
socket_object, address = s.accept()
print("Connection Established\n")
print("Sending file...")
f = open("cds.pdf", 'rb')
while f.read(1024):
socket_object.send(f.read(1024))
print("Files Send")
f.close()
socket_object.close()
s.close()
CLIENT SCRIPT:
import socket
import os
from ftplib import FTP
ftp = FTP()
s = socket.socket()
host = "192.168.43.16"
port = 9999
s.connect((host, port))
#ftp.connect(host = "192.168.43.16", port = 9999)
print("Receiving data...")
f = open("cds_copy.pdf", 'wb')
while True:
ftp.retrbinary('RETR cds_copy.pdf', f.write, 1024)
ftp.storbinary('STOR cds_copy.pdf', open('cds_copy.pdf', 'rb'))
print("File Collected")
ftp.quit()
f.close()
s.close()
ERROR:
$python3 client.py
Receiving data...
Traceback (most recent call last):
File "client.py", line 17, in <module>
ftp.retrbinary('RETR cds_copy.pdf', f.write, 1024)
File "/usr/lib/python3.6/ftplib.py", line 441, in retrbinary
self.voidcmd('TYPE I')
File "/usr/lib/python3.6/ftplib.py", line 277, in voidcmd
self.putcmd(cmd)
File "/usr/lib/python3.6/ftplib.py", line 199, in putcmd
self.putline(line)
File "/usr/lib/python3.6/ftplib.py", line 194, in putline
self.sock.sendall(line.encode(self.encoding))
AttributeError: 'NoneType' object has no attribute 'sendall'
Unable to figure out the error.
Any suggestions will be helpful. Thanking You.