不允许发送或接收数据的请求(UDP file-trasnfer)

时间:2016-02-12 18:13:57

标签: sockets python-3.x udp file-transfer

我正在尝试制作一个简单的文件服务器。我的代码的问题是当用户在客户端上输入文件名并发送到服务器时,服务器应该响应为' EXISTS'或者' NOT EXISTS'回到客户端。我在服务器端插入了一个打印命令,如果客户端请求的文件存在,则打印存在且工作正常,但服务器无法将响应发送回客户端,以便客户端可以下载该文件。以下是我的客户端和服务器代码以及服务器端的Traceback错误。请帮助我成为python的新手。

Server.py

import socket
import threading
import os

def retrvfile(name, sock):
    filename = sock.recv(1024)
    if os.path.isfile(filename):
        print("exists")
        sock.send("EXISTS".encode())
        userresponse = s.recv(1024)
        if userresponse[:2] == 'OK':
                with open(filename, 'rb') as f:
                        bytestosend = f.read(1024)
                        sock.send(bytestosend)
                        while bytestosend != "":
                                bytestosend = f.read(1024)
                                sock.send(bytestosend)

    else:
            print("do not exists")
            sock.send("error".encode())

    sock.close()

def main():
    HOST = '127.0.0.1'
    PORT = 9090

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((HOST, PORT))
    print ("listening")
    print ("server started")
    while True:
            t = threading.Thread(target=retrvfile, args=("retrThread", s))
            t.start()
s.close    

if __name__ == '__main__':
    main()

Client.p

import socket
import sys
def main():
            HOST = '127.0.0.1'
            PORT = 9090

            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect((HOST, PORT))

            print ("UDP target IP:", HOST)
            print ("UDP target port:", PORT)

            filename = input("filename?")
            if filename != 'q':
                s.send(filename.encode())
                data = s.recv(1024)
                if data[:6] == 'EXISTS':
                    filesize = long(data[6:])
                    message = input("file exists, " + str(filesize)+ "Bytes, dowbkoad? (y/n)")
                    if message == 'y':
                                    s.send('ÓK')
                                    f = open('new' + filename, 'wb')
                                    data = s.recv(1024)
                                    totalrecv = len(data)
                                    f.write(data)

                                    while totalrecv < filesize:
                                        data = s.recv(1024)
                                        totalrecv += len(data)
                                        f.write(data)
                                        print ("done")
                                        print ("download complete")
                else:
                                print ("file does not exist")
            s.close()

if __name__ == '__main__':
        main()

回溯如下

exists
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Users\Saqib\AppData\Local\Programs\Python\Python35-32 \lib\threading.py", line 914, in _bootstrap_inner
    self.run()
  File "C:\Users\Saqib\AppData\Local\Programs\Python\Python35-32\lib\threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/Saqib/Desktop/proj/server.py", line 9, in retrvfile
    sock.send("EXISTS".encode())
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

0 个答案:

没有答案