Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file doesn't exist
#1
I am trying to display an image through socket programming,
I have a server and a client file, and they seem to run fine,
however, once I write my file in the console (see input command),
I always obtain the message 'file doesn't exist'.
I am not sure why, because the files I use are in the same directory as
the python files.
Any ideas?
#SERVER
import socket
import threading
import os

from idlelib.iomenu import encoding


def retrieve_file(name, sock):
    filename=sock.recv(1024)
    if os.path.isfile(filename):
        sock.send( ("EXISTS" + str(os.path.getsize(filename))).encode(encoding) )
        user_response = sock.recv(1024)
        if user_response[:2] == 'OK':
            with open(filename, 'rb') as f:
                send_bytes=f.read(1024)
                sock.send(send_bytes)
                while send_bytes !='':
                    send_bytes= f.read(1024)
                    sock.send(send_bytes)
    else:
        sock.send(b"error, file does not exist")
    sock.close()


def Main():
    host = "127.0.0.1"
    port = 5000

    s=socket.socket()
    s.bind((host,port))

    s.listen(5)

    print("server started...")

    while True:
        c, addr= s.accept()
        print("client connected ip>:" + str(addr))
        t=threading.Thread(target=retrieve_file, args=("retrThread",c))
        t.start()

    s.close()

if __name__ == '__main__':
    Main()
#CLIENT

import socket

def Main():
    host = "127.0.0.1"
    port = 5000

    s=socket.socket()
    s.connect((host,port))


    filename= input("enter file name ->")
    if filename != 'q':
        s.send(filename.encode())
        data= s.recv(1024)
        if data[:6] == 'EXISTS':
            filesize = int(data[:6])
            message= input("this file exists " + str(filesize) + "bytes, download(Y/N)?")
            if message == 'Y':
                s.send('OK')
                f=open(filesize, 'wb')
                data=s.recv(1024)
                total_received=len(data)
                f.write(data)
                while total_received < filesize:
                    data=s.recv(1024)
                    total_received += len(data)
                    f.write(data)
                print("download complete")

        else:
            print("file doesn't exist")
    s.close()


if __name__ == '__main__':
    Main()
Reply


Messages In This Thread
file doesn't exist - by mcgrim - Oct-23-2019, 09:09 PM
RE: file doesn't exist - by Larz60+ - Oct-23-2019, 11:35 PM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 06:58 AM
RE: file doesn't exist - by Gribouillis - Oct-24-2019, 07:20 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 08:25 AM
RE: file doesn't exist - by buran - Oct-24-2019, 08:33 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 08:46 AM
RE: file doesn't exist - by buran - Oct-24-2019, 08:50 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 09:02 AM
RE: file doesn't exist - by buran - Oct-24-2019, 09:08 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 09:13 AM
RE: file doesn't exist - by buran - Oct-24-2019, 09:24 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 09:25 AM
RE: file doesn't exist - by buran - Oct-24-2019, 09:31 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 09:36 AM
RE: file doesn't exist - by buran - Oct-24-2019, 10:07 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 10:10 AM
RE: file doesn't exist - by buran - Oct-24-2019, 10:19 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 10:23 AM
RE: file doesn't exist - by buran - Oct-24-2019, 10:37 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 10:42 AM
RE: file doesn't exist - by buran - Oct-24-2019, 10:46 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 10:51 AM
RE: file doesn't exist - by buran - Oct-24-2019, 10:57 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:11 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:14 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:20 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:23 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:29 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:30 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:31 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:33 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:38 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:52 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:53 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:56 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 11:57 AM
RE: file doesn't exist - by buran - Oct-24-2019, 11:58 AM
RE: file doesn't exist - by mcgrim - Oct-24-2019, 12:39 PM
RE: file doesn't exist - by buran - Oct-24-2019, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML file uploaded through python screen doesn't look as expected miker2808 6 5,153 Aug-04-2018, 02:05 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020