1

我是 python 新手,发现很难理解如何使用带有 tcp 连接的套接字发送文件我在另一个似乎有用的问题中发现了这段代码

客户端

def _sendFile(self, path):
    sendfile = open(path, 'rb')
    data = sendfile.read()

    self._con.sendall(encode_length(len(data))) # Send the length as a fixed size message
    self._con.sendall(data)


    # Get Acknowledgement
    self._con.recv(1) # Just 1 byte

服务器端

def _recieveFile(self, path):
    LENGTH_SIZE = 4 # length is a 4 byte int.
    # Recieve the file from the client
    writefile = open(path, 'wb')
    length = decode_length(self.con.read(LENGTH_SIZE) # Read a fixed length integer, 2 or 4 bytes
    while (length):
        rec = self.con.recv(min(1024, length))
        writefile.write(rec)
        length -= sizeof(rec)

    self.con.send(b'A') # single character A to prevent issues with buffering

现在我对这段代码有两个问题首先

self._con.sendall(encode_length(len(data)))

在这一行中,它给了我一个错误,说 encode_length 是未定义的

其次,这些是发送和接收文件的函数我在哪里调用它们我是否首先形成一个 TCP 连接然后调用这些函数以及如何调用它们,如果我直接调用它们它会给我一个错误在客户端说 _sendFile(self , path) 接受两个参数(因为我没有传递 self 只是路径)

第三,我使用 os 库中的函数来获取完整路径,所以我正在调用函数

_sendFile(os.path.abspath("file_1.txt"))

这是传递论点的正确方法吗

对不起,我知道这个问题非常基础和蹩脚,但在网上任何地方我基本上都可以得到这个功能,但不知道如何调用它

现在这就是我调用函数的方式

serverIP = '192.168.0.102'
serverPort = 21000

clientSocket = socket(AF_INET, SOCK_STREAM)

message = "Want to Backup a Directory"


clientSocket.connect((serverIP, serverPort))

_sendFile(os.path.abspath("file_1.txt"))

这基本上是错误的

我为客户端和服务器使用同一台计算机

使用终端在 Ubuntu 上运行 Python

4

1 回答 1

3

第一个问题:

这是因为您根本没有定义函数encode/decode_lenght


第二个问题:

你的功能是:def _sendFile(self, path): ....
你知道怎么用self吗?它在课堂上使用。所以定义它没有self,或使用类:

例子:

from socket import *
class Client(object):

    def __init__(self):

        self.clientSocket = socket(AF_INET, SOCK_STREAM)

    def connect(self, addr):

        self.clientSocket.connect(addr)

    def _sendFile(self, path):

        sendfile = open(path, 'rb')
        data = sendfile.read()

        self._con.sendall(encode_length(len(data))) # Send the length as a fixed size message
        self._con.sendall(data)


        # Get Acknowledgement
        self._con.recv(1) # Just 1 byte

>>> client = Client()
>>> client.connect(("192.168.0.102", 21000))
>>> client._sendFile(os.path.abspath("file_1.txt")) # If this file is in your current directory, you may just use "file_1.txt"

和(几乎)相同Server


在哪里定义这些函数?在代码中!你的函数应该做什么?
好的,举个例子:

def encode_length(l):

    #Make it 4 bytes long
    l = str(l)
    while len(l) < 4:
        l = "0"+l 
    return l

# Example of using
>>> encode_length(4)
'0004'
>>> encode_length(44)
'0044'
>>> encode_length(444)
'0444'
>>> encode_length(4444)
'4444'

关于自己:

一点点:

self重定向到您当前的对象,例如:

class someclass:
    def __init__(self):
        self.var = 10
    def get(self):
        return self.var

>>> c = someclass()
>>> c.get()
10
>>> c.var = 20
>>> c.get()
20
>>> someclass.get(c)
20
>>>

如何someclass.get(c)工作?在执行时,someclass.get(c)我们不会创建. someclass当我们.get()从一个someclass实例调用时,它会自动设置self为我们的实例对象。所以someclass.get(c)==c.get() 如果我们尝试做, 没有someclass.get()定义self,所以会报错: TypeError: unbound method get() must be called with someclass instance as first argument (got nothing instead)


您可以使用装饰器来调用类的函数(而不是它的实例!):

class someclass:
    def __init__(self):
        self.var = 10
    def get(self):
        return 10 # Raises an error
    @classmethod
    def get2(self):
        return 10 # Returns 10!

抱歉我的解释不好,我的英语不完美


以下是一些链接:

服务器.py

客户端.py

于 2013-10-16T19:32:58.353 回答