-1

i need to write a server and a cliet which do 4 things 1.send the time the message was recived 2.get all the files in a folder 3.copy a file from that folder and create a new file(txt) in another folder 4. close the progrem

clint:

    import socket
    import os
    import pickle
    s=socket.socket()
    addr=('127.0.0.1',8200)
    s.connect(addr)
    choice="0"
    while choice!="4":
        print s.recv(4068)
        choice=raw_input("please choose a service number:")
        s.send(choice)
        if choice=="3":
            fileName=raw_input("please enter the name of the file you want to download:")
            s.send(fileName)
        data2= s.recv(4068)
        if choice=="1":
            print data2
        elif choice=="2":
            data_arr=pickle.loads(data2)
            print data_arr
        elif choice=="3":
            text=s.recv(4068)
            if text=="no file with this name":
                print text
            else:          
                newFile=open("newFile.txt","w")
                newFile.write(text)
                newFile.close()       

    print "thanks for using our conection"
    s.close()

server:

import socket
import time
import os
import pickle
def main():
    path="C:\hello"
    s=socket.socket()
    addr=('127.0.0.1',8200)
    s.bind(addr)
    s.listen(5)
    s2,addr2=s.accept()
    choice="0"
    while choice!="4":
        s2.send("1.Get the time of the message\r\n2.Get list of files\r\n3.Download file\r\n4.Close") 
        choice= s2.recv(4068)
        if choice=="1":
            s2.send(time.strftime("%H:%M:%S"))
        if choice=="2":
            dirs=os.listdir(path)
            data_string = pickle.dumps(dirs)
            s2.send(data_string)    
        if choice=="3":
              fileName=s2.recv(4068)
              os.chdir(path)
              dirs=os.listdir(path)
              for file in dirs:
                  if fileName in dirs:
                      f=open(fileName,'r')
                      text=f.read()
                      **s2.send(text)**
                      f.close()
                  else:
                      **s2.send("no file with this name")**
    s.close()
    s2.close()



if __name__ == '__main__':
    main()

the problem is that i tried it on another pc and the progrem just didnt send the message(in Bold) and i cant understand why because the code works on an another pc and after a few tests i know that it found the file and everything is okay, it just doesnt send the message(the .txt file is not empty)

  • This seems problematic, is there any reason you are choosing to use a raw TCP socket rather than something like HTTP? There are a number of frameworks which would make that trivial. – iLoveTux Oct 27 '16 at 18:41
  • im a student, and im just learning sockets, that all that we have learnt and we were asked to write it that way.. – dani borisenko Oct 27 '16 at 18:59
  • well, I can't ethically help too much with homework, but something I did notice is that you are calling `sock.read()` like its definitely going to have data. This is not always the case. Network latency exists and it sucks...you might have to wait for the data to come in...a `while` loop might help in this case. – iLoveTux Oct 27 '16 at 19:06
  • i tried to put a timer for a second and it didnt really help, its not really homework also, that just the way we were thought and i dont know another way to write it... the problem is that when i try it on my pc it works just fine no problems at all, but when i tried to show it to a friend in class it just didnt send anything with `s2.send()` – dani borisenko Oct 27 '16 at 19:14
  • Do you leave it bound to `127.0.0.1`? – iLoveTux Oct 27 '16 at 19:31
  • yeah, its local ip at every pc, i use the same local ip and port in the computer in class, i just found out the gmail can do something weird with your code and lines, could it be that? – dani borisenko Oct 27 '16 at 19:46
  • the message that i recive is the one above, the first one with the services that send right after the while. – dani borisenko Oct 27 '16 at 19:49

1 Answers1

0

Based on your code, you checked whether fileName in dirs or not. You implemented both if and else conditions. One thing you didn't check is dirs itself.

You mentioned that the code works on your machine but not on a 2nd machine. My guess is dirs on 2nd machine is empty. Therefore the code ignores all code for fileName in dirs:

You can add debug lines to check whether dirs is empty

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125