Exemplo n.º 1
0
    def do_login(self):
        username = self.username.get()
        password = self.password.get()
        if not username:
            messagebox.showerror("Error", "username can not be blank")
            return
        if not password:
            messagebox.showerror("Error", "password can not be blank")

        send_message(self.s, MessageType.login, [username, password])
        print("登录请求已发送")

        msg = recv_message(self.s)
        if not msg:
            messagebox.showerror("Error", "connection wrong")
            self.destroy_window()
            return

        if msg[type] == MessageType.loginFailed:
            messagebox.showerror("登录失败", "用户名或密码错误!")
            return

        if msg[type] == MessageType.loginSucc:
            info.current_user = username
            print("登录成功")
            self.master.destroy()
            homepage = Toplevel(info.tk_root, takefocus=True)
            HomePageForm(homepage)
            return
Exemplo n.º 2
0
def send_list(s, parameters):
    book_list = os.listdir('./server/books')
    for i in range(len(book_list)):
        book_list[i] = book_list[i].strip('.txt')
    send_message(s, MessageType.bookList, book_list)
    print("已发送书籍列表!")
    return
Exemplo n.º 3
0
    def do_signup(self):
        username = self.username.get()
        password = self.password.get()
        password_confirm = self.password_confirm.get()

        if not username:
            messagebox.showerror("Error", "username can not be blank")
            return
        if not password or not password_confirm:
            messagebox.showerror("Error", "password can not be blank")
            return
        if password != password_confirm:
            messagebox.showerror("Error", "passwords are not the same")
            return

        send_message(self.s, MessageType.signup, [username, password])
        msg = recv_message(self.s)
        if not msg:
            messagebox.showerror("Error", "connection wrong")
            return
        if msg['type'] == MessageType.usernameTaken:
            messagebox.showerror("Error", "the username has been used")
            return
        if msg['type'] == MessageType.signupSucc:
            messagebox.showerror("注册成功!")
            self.master.destroy()
            return
Exemplo n.º 4
0
def update_bookmark(s, parameters):
    info = parameters.split('&')
    user_name = info[0]
    book_name = info[1]
    page_num = info[2]

    # 检查该书是否存在
    book_list = os.listdir('./server/books')
    for i in range(len(book_list)):
        book_list[i] = book_list[i].strip('.txt')
    if book_name not in book_list:
        send_message(s, MessageType.noBook)
        return

    # 修改书签
    with open('./server/users.txt', 'r', encoding='utf-8') as f:
        users = f.readline()
        for i in range(len(users)):
            user = users[i].split('|')
            if user[0] == user_name:
                if book_name in user:
                    index = user.index(book_name)
                    user[index + 1] = str(page_num)
                    users[i] = '|'.join(user)
                else:
                    users[i] = users[i] + '|' + book_name + '|' + str(page_num)
                break

    with open('./server/users.txt', 'w', encoding='utf-8') as f:
        users = '\n'.join(users) + '\n'
        f.write(users)

    return
Exemplo n.º 5
0
 def download(self):
     path = askdirectory()
     if not path:
         return
     bookname = self.book_list.get(self.book_list.curselection())
     send_message(self.s, MessageType.download, bookname)
     print("下载中……")
     recv_file(self.s, path + '/' + bookname + '.txt')
     print("下载成功!")
     return
Exemplo n.º 6
0
def send_book(s, parameters):
    book_list = os.listdir('./server/books')
    for i in range(len(book_list)):
        book_list[i] = book_list[i].strip('.txt')
    if parameters not in book_list:
        send_message(s, MessageType.noBook)
        return
    send_file(s, './server/books/' + parameters + '.txt')
    print("书籍已发送!")
    return
Exemplo n.º 7
0
def login(s, parameters):
    parameters[0] = parameters[0].strip().lower()  # 删掉开头结尾的字符,并且转换为小写字母
    with open('./server/users.txt', 'r', encoding='utf-8') as f:
        users = f.read().splitlines()
        for user in users:
            user = user.split('|')
            if parameters[0] == user[0] and parameters[1] == user[1]:
                send_message(s, MessageType.loginSucc)
                print("用户名和密码正确,已发送登录成功信息!")
                return
Exemplo n.º 8
0
 def get_booklist(self):
     send_message(self.s, MessageType.requireList)
     msg = recv_message(self.s)
     if not msg:
         messagebox.showerror("Error", "connection wrong")
         return
     if msg['type'] == MessageType.bookList:
         return msg['parameters']
     else:
         messagebox.showerror("请求失败", "服务器未返回书籍列表")
         return
Exemplo n.º 9
0
 def show_page(self):
     send_message(self.s, MessageType.requirePage, [self.bookname, self.page_num])
     msg = recv_message(self.s)
     if not msg:
         messagebox.showerror("Error", "Connection wrong!")
     elif msg['type'] == MessageType.sendPage:
         self.chapter_num = self.get_chapter()
         self.chapbtn['text'] = self.chapter[self.chapter_num][0]
         self.pagebtn['text'] = str(self.page_num + 1) + '/' + str(self.total_num + 1)
         self.text.delete('1.0', 'end')
         if msg['parameters'][0] == '#':
             msg['parameters'] = msg['parameters'][1:]
         self.text.insert(1.0, msg['parameters'])
     else:
         messagebox.showerror("Error", "Can not jump to the page {}".format(msg['type']))
     return
Exemplo n.º 10
0
def signup(s, parameters):
    parameters[0] = parameters[0].strip().lower()
    # 检查用户名是否被占用
    with open('./server/users.txt', 'r', encoding='utf-8') as f:
        users = f.read().splitlines()
        for user in users:
            user = user.split('|')
            if parameters[0] == user[0]:
                send_message(s, MessageType.usernameTaken)
                return

    # 用户名未被占用,添加到用户列表最后
    with open('./serve/users.txt', 'a+', encoding='utf-8') as f:
        f.write(parameters[0] + '|' + parameters[1] + '\n')
    send_message(s, MessageType.signupSucc)
    return
Exemplo n.º 11
0
    def start_read(self):
        # 查找书签
        send_message(self.s, MessageType.startRead, self.user + '&' + self.bookname)
        msg = recv_message(self.s)
        if msg['type'] == MessageType.noBook:
            messagebox.showerror("Error", "No such book!")
            return
        elif msg['type'] == MessageType.pageNum:
            self.page_num = msg['parameters']
        else:
            messagebox.showerror("Error", "Can not receive bookmark! {}".format(msg['type']))
            return

        # 接收总页数
        msg = recv_message(self.s)
        if msg['type'] == MessageType.totalPage:
            self.total_num = msg['parameters']
        else:
            messagebox.showerror("Error", "Can not receive total page! {}".format(msg['type']))
            return

        # 接收章节列表
        msg = recv_message(self.s)
        if msg['type'] == MessageType.sendChapter:
            self.chapter = msg['parameters']
            self.total_chapter = len(self.chapter)
            self.chapter_num = self.get_chapter()
            self.chapbtn['text'] = self.chapter[self.chapter_num]
        else:
            messagebox.showerror("Error", "Can not receive chapter list! {}".format(msg['type']))
            return

        # 接收书签那一页
        msg = recv_message(self.s)
        if not msg:
            messagebox.showerror("Error", "Connection wrong...")
        elif msg['type'] == MessageType.sendPage:
            if msg['parameters'][0] == '#':
                msg['parameters'] = msg['parameters'][1:]
            self.text.insert(1.0, msg['parameters'])
        else:
            messagebox.showerror("Error", "Can not receive the text! {}".format(msg['type']))
        return
Exemplo n.º 12
0
def start_read(s, parameters):
    info = parameters.split('&')
    user_name = info[0]
    book_name = info[1]

    # 检查该书是否存在
    book_list = os.listdir('./server/books')
    for i in range(len(book_list)):
        book_list[i] = book_list[i].strip('.txt')
    if book_name not in book_list:
        send_message(s, MessageType.noBook)
        return

    # 查找书签
    page_num = 0  # 初始化书签为0
    with open('./server/users.txt', 'r', encoding='utf-8') as f:
        users = f.read().splitlines()
        for user in users:
            user = user.split('|')
            if user[0] == user_name:
                if book_name in user:
                    index = user.index(book_name)
                else:
                    index = -1
                if index != -1:
                    page_num = int(user[index + 1])
                    break
    send_message(s, MessageType.pageNum, page_num)

    # 获得总页数和章节列表
    total_page = 0
    chapter = []
    chapter.append(['书名和作者', 0])
    i = 1
    with open('./server/books/' + book_name + '.txt', 'r',
              encoding='utf-8') as f:
        line = f.readline()
        while line:
            s = ''
            s += line
            line = f.readline()
            while line:
                if line[0] == '#':
                    break
                s += line
                line = f.readline()
            total_page += math.ceil(len(s) / 1000)
            chapter.append([line[1:-1], total_page])
    send_message(s, MessageType.totalPage, total_page)
    send_message(s, MessageType.sendChapter, chapter)

    send_page(s, book_name + '&' + str(page_num))
Exemplo n.º 13
0
def send_page(s, parameters):
    info = parameters.split('&')
    book_name = info[0]
    page_num = info[1]

    # 检查该书是否存在
    book_list = os.listdir('./server/books')
    for i in range(len(book_list)):
        book_list[i] = book_list[i].strip('.txt')
    if book_name not in book_list:
        send_message(s, MessageType.noBook)
        return

    # send a page
    book_path = './server/books/' + book_name + '.txt'
    with open(book_path, 'r', encoding='utf-8') as f:
        num = 0
        j = 0
        line = f.readline()
        while num <= page_num:
            s = ''
            if line:
                s += line
                line = f.readline()
                while line:
                    if line[0] == '#':
                        break
                    s += line
                    line = f.readline()
            if num + math.ceil(len(s) / 1000) - 1 < page_num:
                num += math.ceil(len(s) / 1000)
                continue
            elif num + math.ceil(len(s) / 1000) - 1 == page_num:
                j = 1000 * (math.ceil(len(s) / 1000) - 1)
                num += math.ceil(len(s) / 1000)
            else:
                j = 1000 * (page_num - num)
                num = page_num
                break
        send_message(s, MessageType.sendPage, s[j:j + 1000])
    return
Exemplo n.º 14
0
 def update_bookmark(self):
     send_message(self.s, MessageType.updateBookmark, [self.user, self.bookname, self.page_num])
     self.master.destroy()
     return