当前位置: 首页>>代码示例>>Python>>正文


Python Server.send_file方法代码示例

本文整理汇总了Python中server.server.Server.send_file方法的典型用法代码示例。如果您正苦于以下问题:Python Server.send_file方法的具体用法?Python Server.send_file怎么用?Python Server.send_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在server.server.Server的用法示例。


在下文中一共展示了Server.send_file方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FileService

# 需要导入模块: from server.server import Server [as 别名]
# 或者: from server.server.Server import send_file [as 别名]
class FileService(object):
    """docstring for FileService"""

    def __init__(self, port, ds_host, ds_port):
        super(FileService, self).__init__()
        self.dir = ''
        self.port = port
        self.ds_host = ds_host
        self.ds_port = ds_port
        self.server = Server(port, self._handler)

    def announce_server(self):
        sock = self.server.send_as_client(self.ds_host, self.ds_port, "INSERT " + self.server.host + ":" + str(self.port) + " " + "1")
        data = sock.recv(1024)
        if data.decode('utf-8') != response200():
            print("Error with Directory Server: ", data)
        sock.shutdown(socket.SHUT_WR)
        sock.close()
        #TODO Add to allow exsting files or remove

    def _open_handler(self, conn, path):
        filename = self.dir + path
        if not os.path.isfile(filename):
            self.server.error(conn, 404, response404())
        else:
            f = open(filename, "rb")
            pdb.set_trace()
            self.server.send_file(conn, f)
            conn.shutdown(socket.SHUT_WR)
            conn.close()

    def _create_handler(self, conn, path):
        file_path = os.path.dirname(path)
        if not os.path.isdir(file_path):
            self.server.error(conn, 405, response405())
        else:
            self.server.respond(conn, response200())
            exists = os.path.isfile(self.dir + path)
            f = open(self.dir + path, "wb")
            conn.recv_file(f)
            f.close()
            self.server.respond(conn, response200())
            # TODO: send to other replication managers
            # if not exists:
            #

    def _mkdir_handler(self, conn, path):
        newdir = str(self.dir + path.strip('/'))
        basedir = os.path.dirname(newdir)
        if not os.path.isdir(basedir):
            self.server.error(conn, 404, response404())
        else:
            try:
                os.makedirs(newdir)
                self.server.respond(conn, response200())
                # self._advertise_buffer.add(path.strip('/'), ObjectBuffer.Type.directory)
                # TODO: send to replication manager
            except Exception as e:
                if e.errno != errno.EEXIST:
                    raise
                else:
                    conn.send()

    def _delete_handler(self, conn, path):
        pdb.set_trace()
        obj = str(self.dir + path).rstrip('/')
        if os.path.isdir(obj):
            shutil.rmtree(obj)
            self.server.respond(conn, response200())
            # self._advertise_buffer.add(path, ObjectBuffer.Type.deleteDirectory)
        elif os.path.isfile(obj):
            os.remove(obj)
            self.server.respond(conn, response200())
            # self._advertise_buffer.add(path, ObjectBuffer.Type.deleteFile)
        else:
            self.server.error(conn, 404, "not found!")

    def _handler(self, sock):
        LF = "\n"
        try:
            data = sock.recv(1024)
            msg = data.decode('utf-8')
            msg = msg.replace('$', ' ')
            if 'KILL_SERVICE' in msg:
                exit()
            elif msg[:4] == "HELO":
                response = ["IP:" + self.server.host, "Port:" + str(self.port), "StudentID:" + self.server.studentId]
                fullResponse = msg + LF.join(response)
                print(response)
                sock.send(fullResponse.encode('utf-8'))
            elif msg[:4] == "OPEN":
                temp = msg.split()
                self._open_handler(sock, temp[1])
            elif msg[:6] == "CREATE":
                temp = msg.split()
                self._create_handler(sock, temp[1])
            elif msg[:5] == "MKDIR":
                temp = msg.split()
                self._mkdir_handler(sock, temp[1])
            elif msg[:6] == "DELETE":
#.........这里部分代码省略.........
开发者ID:adonoghuTcd,项目名称:distributed-file-system,代码行数:103,代码来源:fileserver.py


注:本文中的server.server.Server.send_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。