用Python进行套接字编程以在Windows中传输文件

时间:2018-10-12 19:50:59

标签: python sockets

我在python中编写了一个用于与客户端服务器通信的程序(引自Internet bogotobogo.com)。当我在Windows 10中运行它时,显示以下错误: ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了连接

服务器代码:

    import socket
    PORT = 60000
    HOST = '127.0.0.1'
    with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
        s.bind((HOST,PORT))
        s.listen(5)
        print ("Server Listening...")
        while True:
            conn,addr = s.accept()
            print("Got Connection from ",addr)
            data = conn.recv(1024)
            print("Server received", repr(data))
            filename = 'mytext.txt'
            f = open(filename,'rb')
            l = f.read(1024)
            while(l):
                conn.send(l)
                print('Sent ', repr(1))
                l = f.read(1024)
                f.close()
                print("Done Sending")
                conn.send("Thank you for connecting")
                conn.close()

客户代码:

    import socket
    HOST = '127.0.0.1'
    PORT = 60000
    with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
        s.connect((HOST,PORT))
        s.send("Hello Server")
        with open('received file','wb') as f:
            print('opened file')
            while True:
                print('receiving data...')
                data = s.recv(1024)
                print('data=%s',(data))
                if not data:
                    break
                f.write(data)
            f.close()
        print('Successfully get the file')
        s.close()
        print('Connection Closed')

能不能让我知道我哪里出问题了

0 个答案:

没有答案