How to recieve files from a server in python? - python-3.x

I Have made a python script, where you can send files to a remote server and receive them. I can send them to the server but can not seem to retrieve them back I've attached both server and client.py
Server.py
import socket # Import socket module
import os
s = socket.socket()
s.bind(('139.59.173.187', 8010)) # Binds port and IP address
s.listen(3) #wait for client to join
c, addr = s.accept()
def RecvFile():
print ("File is comming ....")
file = c.recv(1024) #recieves file name from client
print(file)
f = open(file,'wb') #open that file or create one
l = c.recv(4096) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = c.recv(4096) #get again until done
print("The file",file,"has been succesfully saved to the server\nConnection from:",addr)
f.close()
def SendFile():
file = c.recv(1024)
print(file)
with open(file,'rb') as f:
c.sendall(f.read())
print("File has been sent to the client:", addr)
main()
def main():
option = str(c.recv(1024), 'utf-8')
print(option)
if option[:1] == "R":
SendFile()
elif option[:2] == "S":
RecvFile()
main()
s.close()
Client.py
import time
import socket
import sys
import urllib.request
#import paramiko
def login():
a = 1
while a == 1:
global user
user = input("Username:")
passw = input("Password:")
with open('User.txt') as f:
for line in f.readlines():
us, pw = line.strip().split("|", 1)
if (user == us) and (passw == pw):
print("login Successful!")
a = a+1
main()
return
print("Incorrect details, Try again!")
def register():
print("You will recieve a generated Username from your Name and age")
regName = input("First Name: ")
regAge = input("Age: ")
regPass = input("Password: ")
regUser = (regName[0:3]+regAge)
with open('User.txt', 'a') as file:
file.writelines(regUser+"|"+regPass+"\n")
print("You have Succesfully registered")
print("Your Username is:"+regUser)
print("Your Password is:"+regPass)
print("!Please keep these credentials secure!")
def main():
print("=========================Welcome to Bluetooth Projector=========================")
option = input("Here are your options:\n--> Press C to connect to the projector\n--> Press F for File System")
x = 1
while x == 1:
if option == "C" or option == "c":
optionc(x)
elif option == "F" or "f":
File_System()
def optionc():
print("Loading.....")
time.sleep(2)
print("Obtaining IP addres.....")
time.sleep(2)
print("Connecting.....")
time.sleep(3)
print("Connected")
x = x+1
def File_System():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('139.59.173.187',8010))
print("=========================Welcome to the File System =========================")
option = input("Here are your options:\n--> Press S to upload files\n--> Press R to retrieve files")
if option == "S" or option == "s":
s.send("S".encode('utf-8'))
file_name = input("Please input the file name\n>>>")
name = input("What would you like it to be saved as?\n>>>")
s.send(name.encode('utf-8'))
with open(file_name,'rb') as f:
s.sendall(f.read())
print("File has been sent to the server")
main()
elif option == "R" or option == "r":
s.send("R".encode('utf-8'))
filename = input("What is the filename you wish to recieve\n>>>")
s.send(filename.encode('utf-8'))
print ("File is comming ....")
f = open(filename,'wb') #open that file or create one
l = s.recv(1024) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = s.recv(1024)
print("The file",filename,"has been succesfully saved to your computer")
f.close()
while True:
check = input("Do you already have an account? Y/N \n ")
if check == "Y" or check == "y":
login()
break
elif check == "N" or check == "n":
register()
break
else:
print("pleae enter either Y or N")
Please look at the Filesystem() in the client file. I think it is receiving the second wave of bytes when getting files that is the problem
an was error is either broken pipe which I fixed.
But now the actually receiving file loop doesn't go through and stops getting the file bytes. I believe this is my error. I am confused however since my code is pretty much identical to sending the file to the server which has no complications.

But now the actually receiving file loop doesn't go through and stops getting the file bytes. I believe this is my error.
You are right. The client gets no indication that there aren't more file contents to come, and thus keeps on waiting in recv forever. To rectify this, you can let the server shut down the connection when the file has been sent, or, if you want the connection to be kept, inform the client about when it has to stop receiving, e. g. by sending it the file length beforehand.

Related

loop doesn't break up when receiving data from client even i wrote break

Hi everyone I decided to create a program that transfer files between client and server, but the problem is the loop won't break its looks like he's still waiting for data to receive, even I wrote (if not data: break)
I received the file successfully, but it's not back to the outer loop any help and thank you.
while True:
###server side
cmd = input("enter msg command :")
client_socket.send(cmd.encode())
if cmd.startswith("download"):
filename = cmd.strip("download ")
with open(filename, 'wb') as f:
print("Downloading file...")
while True:
data = client_socket.recv(4096)
if not data:
break
f.write(data)
print("Successfully downloaded")`
#####clientside
while True:
command = s.recv(1024)
command = command.decode()
if command.startswith("download"):
print("True\n")
filename = command.strip("download ")
with open(filename,'rb') as f:
print("file sending.....")
while True:
data = f.read(4096)
if not data:
break
s.sendall(data)
print("file has sent")

Unicode error for python echo server when "Ctrl+C" pressed

I have created python echo server. The echo server can be started using command
sudo python3.8 echo_server.py
The client can be connected to it using command
telnet localhost 5000
I have implemented some commands like time,exit and network (i.e. if you type 'time' command on telnet client, the server responds with system time.)
For "network" command contain 3 while loops (i.e 1 loop for selecting network type, 2 loop for selecting static\dynamic, 3 loop for configuring ssid/password for wifi settings).
Now while configuring ssid/pwd, I wanted to go back to main command prompt for telnet client (i.e. where we can enter time,network command) on press of "Ctrl+c". But I am not able to handle the "Ctrl+c" on the echo server.I am getting below exception when "Ctrl+c" is pressed on telnet client
Exception in thread MyThread:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "echo_server.py", line 47, in run
tx_data = ""
File "/usr/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Main Terminating...
Please find my echo_server.py file
import socket
import os
import os.path
import subprocess
import time
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
from time import sleep
class MyThread(Thread):
def __init__(self, val):
''' Constructor. '''
Thread.__init__(self)
self.val = val
class Filter:
"""Substitute \n with \r\n in stream output to terminal."""
def __init__(self, file):
self.file = file
def write(self, data):
self.file.write(data.replace("\n", "\r\n"))
def run(self):
while True:
HOST = '' # Symbolic name meaning all available interfaces
PORT = 5000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((HOST, PORT))
s.listen(5)
except socket.error:
time.sleep(5)
continue
conn, addr = s.accept()
file = conn.makefile(mode="rw")
filtered_file =MyThread.Filter(file)
file.flush()
filtered_file.write("Enter 'help' to list all valid commands\n")
while True:
file.write("Command> ")
file.flush()
data = file.readline()
tx_data = ""
if not data: break
data = data.strip()
if not data:
continue
elif data == 'time':
f= os.popen('date')
date=f.read().strip()
tx_data = date + '\n'
elif data == "network":
while True:
file.write("1. WiFi\n")
file.flush()
file.write("2. Ethenet\n")
file.flush()
file.write("3. Exi\n")
file.flush()
file.write("Enter a choice:")
file.flush()
choice = file.readline()
choice = choice.strip()
if choice == "1":
while True:
file.write("1. DHCP\n")
file.flush()
file.write("2. Static\n")
file.flush()
file.write("3. Exit\n")
file.flush()
file.write("Enter a choice:")
file.flush()
subchoice = file.readline()
subchoice = choice.strip()
if subchoice == "1":
while True:
file.write("Enter ssid:")
file.flush()
ssid = file.readline()
ssid = ssid.strip()
file.write("Enter pwd:")
file.flush()
pwd = file.readline()
pwd = pwd.strip()
break
break
elif choice == "2":
break
elif choice == "3":
break
else:
break
elif data == 'help':
tx_data = '''Valid commands are as below:
Enter number against the command for execution
1. time
2. network
3. exit\n'''
elif data == 'exit':
break
else:
tx_data = "Unknown Command: " + data + \
"\nEnter 'help' for list of valid commands\n"
filtered_file.write(tx_data)
#print 'Closing connection with client'
file.close()
conn.close()
# Run following code when the program starts
if __name__ == '__main__':
# Declare objects of My Thread class
my_obj = MyThread(4)
my_obj.setName('MyThread')
# Start running the threads!
my_obj.start()
# Wait for the threads to finish...
my_obj.join()
print('Main Terminating...')
I am not sure how to handle "Ctrl+c" so that control is back at the prompt when we can enter command.
Please let me know if any one has any suggestion to resolve this.
Here is my version of your program:
# echo_server.py
import socket
import os
import os.path
import subprocess
import time
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
from time import sleep
def getcommand(thisfile, thisprompt, stripped=True):
thisfile.write(thisprompt)
thisfile.flush()
command = thisfile.readline()[:-1]
# print(f"{len(command)} chars received") # just for debug
# for i in command:
# print(ord(i))
if stripped:
return command.strip()
return command
class MyThread(Thread):
def __init__(self, val):
''' Constructor. '''
Thread.__init__(self)
self.val = val
class Filter:
"""Substitute \n with \r\n in stream output to terminal."""
def __init__(self, file):
self.file = file
def write(self, data):
self.file.write(data.replace("\n", "\r\n"))
def run(self):
while True:
HOST = '' # Symbolic name meaning all available interfaces
PORT = 5000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((HOST, PORT))
s.listen(5)
except socket.error:
time.sleep(5)
continue
conn, addr = s.accept()
file = conn.makefile(mode="rw", encoding="UTF-8", errors="ignore")
filtered_file = MyThread.Filter(file)
file.flush()
filtered_file.write("Enter 'help' to list all valid commands\n")
while True:
data = getcommand(file, "Command> ", stripped=False)
tx_data = ""
if not data:
print("No Command!")
break # closing connection
data = data.strip()
if not data:
print("Just Spaces!")
continue
elif data == 'time':
f= os.popen('date')
date=f.read().strip()
tx_data = date + '\n'
elif data == "network":
while True:
file.write("1. WiFi\n")
file.flush()
file.write("2. Ethernet\n")
file.flush()
file.write("3. Exit\n")
file.flush()
choice = getcommand(file, "Enter a choice:")
if choice == "1":
while True:
file.write("1. DHCP\n")
file.flush()
file.write("2. Static\n")
file.flush()
file.write("3. Exit\n")
file.flush()
subchoice = getcommand(file, "Enter a choice:")
if subchoice == "1":
while True:
ssid = getcommand(file, "Enter ssid:")
pwd = getcommand(file, "Enter password:")
if ssid and pwd:
break
break
elif choice == "2":
break
elif choice == "3":
break
elif data == 'help':
tx_data = '''Valid commands are as below:
Enter number against the command for execution
1. time
2. network
3. exit\n'''
elif data == 'exit':
break
else:
tx_data = "Unknown Command: " + data + \
"\nEnter 'help' for list of valid commands\n"
filtered_file.write(tx_data)
print('Closing connection with client')
file.close()
conn.close()
# Run following code when the program starts
if __name__ == '__main__':
# Declare objects of My Thread class
my_obj = MyThread(4)
my_obj.setName('MyThread')
# Start running the threads!
my_obj.start()
# Wait for the threads to finish...
my_obj.join()
print('Main Terminating...')
I made a new function to handle the input of text from the terminal. This function strips the new line char appended by readline() and optionally strips leading and trailing blanks from the input string. This enables your choice to close the connection if the user enters no data, while keeping it open and repeating the prompt if s/he just enters whitespace.
I also used the 'errors' optional argument in conn.makefile(), to avoid the UnicodeDecodeError, and added a line to repeat the input of ssid and password if either is blank. I also removed the else clause when the main choice is "network", to let the user out only by choosing 3.
With this version, there's no need to use Ctrl-C (that also has a meaning as KeyboardInterrupt) and if the user entered it, it wouldn't disrupt the server anymore.
Now, if you press Ctrl-C in the client, you stop receiving the server's output, but if you blindly enter the "exit" choices you get out of the connection gracefully.
Because ctrl-c not a unicode character and _io.TextIOWrapper (your file variable) can't handle it in unicode mode.
You can initialize wrapper in non unicode mode
file = conn.makefile(mode="rw",encoding='latin-1')
This will not disconnect client by ctrl-c and you should handle all special characters by your self (because telnet will still send it)
I changed your code to handle decoder errors as is. This will disconnect client on ctrl-c
while True:
file.write("Command> ")
file.flush()
try:
data = file.readline()
except UnicodeDecodeError as e:
conn.close()
break;
tx_data = ""
#if not data: break <-- This will throw error data undefined
data = data.strip()
Actually better solution not to use text wrapper at all
data = conn.recv(1024)
if not data: break
you have to initialize wrapper in other encoding type, use encoding = "latin-1"

CRUD program to read,write and append the details in txt file

It's a school assignment, an event management system. It will write the data to a txt file and retrieve it. Its mostly a CRUD program but not sure why it is not running. It shows space as an error on VS CODE IDE.
It will Create a customer, ask for the seats that he want to book. He can also delete the seats as per ref number before 24 hours.
import random
print("Welcome to the Event System")
def menu():
print("Choose the number from menu")
print("1:Create Customer")
print("2:Reserve a seat")
print("3.Cancel the seat")
print("4.Exit the Event System")
option = input("put down the number")
return option
def executingmenuinputchoice(option):
if(option == 1):
createcust()
elif(option == 2):
reserveseat()
elif(option == 3):
cancelseat()
elif(option == 4):
exit()
else:
print('you have chose a wrong option')
menu()
def createcust():
print('Provide Customer details')
name = input('Full name? --> ')
pno = input('phone number? -> ')
email = input('Email Id? --> ')
try:
file = open("cust.txt", "r")
lines = file.read().splitlines()
last_lines = lines[-1]
content = last_lines.split()
refno = random.randint(10001, 99999)
file.close()
file = open("cust.txt", "a")
file.write("\n"+" "+name+" "+pno+" "+email+" "+refno)
file.close()
print(refno + 'is your reference number')
print('Added Customer to file')
except IOError:
print("File doesn't exist at location")
except TypeError:
print('input proper data')
return createcust()
def customerexist(refno):
try:
file = open("cust.txt", "r")
for line in file:
content = line.split()
if (refno == int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("File doesn't exist")
file.close()
return False,0
def reserveseat():
referencenumber=input("Enter the Reference Number-->")
refexist =referenceexist(referencenumber)
if(refexist==True):
seatsyouwantbook=input("Number of seats you want to book? ->")
date=datetime.datetime.now()
seats=seats+seatsyouwantbook
newline=""
try:
file=open("cust.txt","r")
lines=file.read().splitlines()
last_linesno=len(lines)
currentLine=1
for line in lines:
content=line.split
if(currentline!=last_linesno):
if(refno==int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("FIle never existed")
file.close()
return False,0
def cancelseat():
try:
file=open("cust.txt","r")
for line in file:
content=line.split()
if (refno==int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("File doesn't exist")
file.close()
return False,0
invalid syntax (<unknown>, line 41)
I want it to run properly so, I can submit it again.
I haven't checked your whole code, but at least got it running to the point that you could further rectify it:-
import random
print("Welcome to the Event System")
def customerexist(refno):
try:
file = open("cust.txt", "r")
for line in file:
content = line.split()
if (refno == int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("File doesn't exist")
file.close()
return False,0
def reserveseat():
referencenumber=input("Enter the Reference Number-->")
refexist =referenceexist(referencenumber)
if(refexist==True):
seatsyouwantbook=input("Number of seats you want to book? ->")
date=datetime.datetime.now()
seats=seats+seatsyouwantbook
newline=""
try:
file=open("cust.txt","r")
lines=file.read().splitlines()
last_linesno=len(lines)
currentLine=1
for line in lines:
content=line.split
if(currentline!=last_linesno):
if(refno==int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("FIle never existed")
file.close()
return False,0
def cancelseat():
try:
file = open("cust.txt","r")
for line in file:
content=line.split()
if (refno==int(content[4])):
file.close()
return True,int(content[5])
except IOError:
print("File doesn't exist")
file.close()
return False,0
def createcust():
print('Provide Customer details')
name = input('Full name? --> ')
pno = input('phone number? -> ')
email = input('Email Id? --> ')
try:
file = open("cust.txt", "r")
lines = file.read().splitlines()
last_lines = lines[-1]
content = last_lines.split()
refno = random.randint(10001, 99999)
file.close()
file = open("cust.txt", "a")
file.write("\n"+" "+name+" "+pno+" "+email+" "+refno)
file.close()
print(refno + 'is your reference number')
print('Added Customer to file')
except IOError:
print("File doesn't exist at location")
except TypeError:
print('input proper data')
return createcust()
def menu():
print("Choose the number from menu")
print("1:Create Customer")
print("2:Reserve a seat")
print("3.Cancel the seat")
print("4.Exit the Event System")
option = input("put down the number")
return option
def executingmenuinputchoice(option):
option = int(option)
if(option == 1):
createcust()
elif(option == 2):
reserveseat()
elif(option == 3):
cancelseat()
elif(option == 4):
exit()
else:
print('you have chose a wrong option')
menu()
executingmenuinputchoice(menu())
REASON FOR ERRORS:-
Your indentation was all over the place, python language
prioritizes indentation as it uses it to figure out a block span, so you should
keep a consistent indentation scheme throughout your code.
Python uses top down approach for ~interpreting the
program, i.e. It only keeps track of the stuff that it had already
encountered. In your code, the function executingmeninputchoice()
and menu() (the two primary functions used for UI) were stacked
above all other function, therefore when you tried to call other
function from these two function, they aren't called. As the program
doesn't know whether these functions exists or not (as it hasn't
encountered them yet)
A logical error existed in function
executingmenuinputchoice(option) as you were trying to take in
input a string and were comparing it with integer values, and
therefore every time the operation failed and the control got shifted
to the else block, therefore every time you got the same output
'you have chose a wrong option' regardless of whether the input was
legal or not
P.S.:- I haven't tested your full code, as this isn't a code for me service, other logical errors may also exist, so I would recommend you to find and fix those too.

Send and recieve files in python

So, I am making something in my computer science class where you can send files to a remote server and that works, but cant recv them back.
SERVER.PY
import socket # Import socket module
import os #imports os module
def RecvFile():
while True: # allows it to always save files and not have to be restarted.
c, addr = s.accept()
print ('Connection Accepted From',addr)# this prints out connection and port
print ("File is comming ....")
file = c.recv(1024) #recieves file name from client
f = open(file,'wb') #open that file or create one
l = c.recv(1024) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = c.recv(1024) #get again until done
print("The file",file,"has been succesfully saved to the server\nConnection from:",addr)
f.close()
def SendFile():
c, addr = s.accept()
print("Connection Accepted From",addr)
filename = c.recv(1024)
if os.path.isfile(filename):
s.send("EXISTS"+str(os.pthat.getsize(filename)))
response = c.recv(1024)
if response[:2] == 'OK':
with open(filename, 'rb') as f:
s.sendall(f.read)
f.close()
else:
s.send("ERR")
s.close()
def main():
s = socket.socket()
s.bind(('139.59.173.187', 8000)) # Binds port and IP address
s.listen(3)
print("Server Started")
while True:
c, addr = s.accept()
option = c.recv(1024)
if option == "S":
RecvFile()
elif option == "R":
SendFile()
else:
s.send("Incorrect input".encode('utf-8'))
main()
#if errors occur
c.close()
I think the server.py is the problem
Client .py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('139.59.173.187',8000))
print("=========================Welcome to the File System =========================")
option = input("Here are your options:\n--> Press S to upload files\n--> Press R to retrieve files")
if option == "S" or option == "s":
s.send("S".encode('utf-8'))
file_name = input("Please input the file name\n>>>")
name = input("What would you like it to be saved as?\n>>>")
s.send(name.encode('utf-8'))
with open(file_name,'rb') as f:
s.sendall(f.read())
print("File has been sent to the server")
main()
elif option == "R" or option == "r":
s.send("R".encode('utf-8'))
filename = input("What is the filename you wish to recieve\n>>>")
s.send(filename.encode('utf-8'))
print ("File is comming ....")
f = open(filename,'wb') #open that file or create one
l = c.recv(1024) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = c.recv(1024) #get again until done
print("The file",filename,"has been succesfully saved to your computer")
f.close()
I have put 7 hours into learning this and the asignment is due in friday. rather than saying the code could you comment how it works etc.
In server.py, SendFile and RecvFile both use a socket object s that they have no access to. I recommend that you pass it as an argument :
def RecvFile(s):
#...
def SendFile(s):
#...
And then in main :
if option == "S":
RecvFile(s)
elif option == "R":
SendFile(s)

Error writing text into a .txt file in python

I am making a program that will
1. Create a text file
2. Allow a password to be stored
3. Allow a password to be changed
4. Add an additional password
5. Delete a specific password
The problem is in def delete():. I put in three passwords on three seperate lines: first, second, third. When I choose to delete password "second", it reprints the list from before, and then prints the new list at the end of the last password.
Here is my code:
import time
def create():
file = open("password.txt", "w")
passwordOfChoice = input("The password you want to store is: ")
file.write(passwordOfChoice)
print ("Your password is: ", passwordOfChoice)
file.close()
time.sleep(2)
def view():
file = open("password.txt","r")
print ("Your password is: ",
"\n", file.read())
file.close()
time.sleep(2)
def change():
file = open("password.txt", "w")
newPassword = input("Please enter the updated password: ")
file.write(newPassword)
print ("Your new password is: ", newPassword)
file.close()
time.sleep(2)
def add():
file = open("password.txt", "a")
extraPassword = input("The password you want to add to storage is: ")
file.write("\n")
file.write(extraPassword)
print ("The password you just stored is: ", extraPassword)
file.close()
time.sleep(2)
def delete():
phrase = input("Enter a password you wish to remove: ")
f = open("password.txt", "r+")
lines = f.readlines()
for line in lines:
if line != phrase+"\n":
f.write(line)
f.close()
print("Are you trying to: ",
"\n1. Create a password?",
"\n2. View a password?",
"\n3. Change a previous password?",
"\n4. Add a password?",
"\n5. Delete a password?",
"\n6. Exit?\n")
function = input()
print("")
if (function == '1'):
create()
elif (function == '2'):
view()
elif (function == '3'):
change()
elif (function == '4'):
add()
elif (function == '5'):
delete()
elif (function == '6'):
print("Understood.", "\nProgram shutting down.")
time.sleep(1)
else:
print("Your answer was not valid.")
print("Program shutting down...")
time.sleep(1)
To show what I meant above, here is my output:
Your password is:
first
second
thirdfirst
third
Can someone please tell me how to fix my def delete(): function so that it will not rewrite the original data? Thanks a ton!
The problem lies with the 'r+' mode. When you use 'r+' you can read and write, sure, but it's up to you to control where in the file you write.
What's happening is you read the file, and the cursor is stuck at the end, so when you write it back, Python dutifully puts your new lines on the end of the file.
See the docs about file methods; you're looking for something like seek.

Resources