0% found this document useful (0 votes)
35 views1 page

Server Coding

This Python code connects to a server using a socket, sends a filename to the server, receives a response indicating if the file exists, and if it does, downloads the file from the server in 1024 byte chunks and saves it locally. It prints download progress percentages and notifies when complete. The code connects to a host and port, sends the filename, receives a response, and if the file exists, gets the filesize, prompts the user, sends an OK, receives the file in chunks writing to a new file, and prints progress until finished.

Uploaded by

afzal hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

Server Coding

This Python code connects to a server using a socket, sends a filename to the server, receives a response indicating if the file exists, and if it does, downloads the file from the server in 1024 byte chunks and saves it locally. It prints download progress percentages and notifies when complete. The code connects to a host and port, sends the filename, receives a response, and if the file exists, gets the filesize, prompts the user, sends an OK, receives the file in chunks writing to a new file, and prints progress until finished.

Uploaded by

afzal hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import socket

def Main():
host = '[Link]'
port = 5000

s = [Link]()
[Link]((host, port))
nameoffile='This is Tayyab nice to meet you'
filename = bytes(nameoffile,'utf-8')

if filename != 'q':
[Link](filename)
data = [Link](1024)
if data[:6] == 'EXISTS':
filesize = long(data[6:])
message = raw_input("File exists, " + str(filesize) +"Bytes, download? (Y/N)?
-> ")
if message == 'Y':
[Link]("OK")
f = open('new_'+filename, 'wb')
data = [Link](1024)
totalRecv = len(data)
[Link](data)
while totalRecv < filesize:
data = [Link](1024)
totalRecv += len(data)
[Link](data)
print ("{0:.2f}".format((totalRecv/float(filesize))*100)+ "% Done")
print ("Download Complete!")
[Link]()
else:
print ("File Does Not Exist!")

[Link]()

if __name__ == '__main__':
Main()

You might also like