猫史档案馆


【Python作品分享】Python服务器管理端框架【作品秀】

用户:HFYHTVS_HFYHTVS_查看:2 回复:8 评论:2 创建时间:2020-06-04T12:57:00


【作品展示】

center_image

 

【作品介绍】

并没有连上python web

仅仅是Python终端的管理框架,没有做UI 太麻烦

准备做个喵天室

这个就算是框架了,对接你们自己搞,类和输入端我都做好了

 

【作品源代码】

import time
import sys
from colorama import *
import random
import threading
import socket
import json
import os
import select

'''
This is not a real server.
Just a server simulation to test!
So you can do what ever you want. XD
Program By Hfyhtvs
'''
#------ Start Coding -------

def create_message_cube(Message,cube_width,cube_lines): #cube_lines 是 cube 高度,单位:行
    length = len(str(Message))
    print('-'*cube_width)
    for i in range(cube_lines):
        if abs(cube_lines//2) == i:
            if Is_CN_Word(Message):
                print('=', ' '*abs(cube_width//2 - 3 -  length),\
                f'{Message}', ' '*abs(cube_width//2 - 3 - length),'=')
            elif not Is_CN_Word(Message):
                print('=', ' '*abs(cube_width//2 - 3 -  length//2),\
                f'{Message}', ' '*abs(cube_width//2 - 3 - length//2),'=')
        else:
            print('=',' '*(cube_width - 4),'=')
    print('-'*cube_width)


def Is_CN_Word(word):
    for ch in word:
        if '\u4e00' <= ch <= '\u9fff':
            return True     #是中文
    return False            #是数字或英文

class Server_say():
    def __init__(self):
        super().__init__()
        self.set_up()

    def set_up(self):
        self.userlist = []
        create_message_cube('- Server Simulation -', 41, 3)
        print(Fore.YELLOW+ Style.BRIGHT+ '--Test.ServerCore ')
        for i in range(0,101):
            print(Fore.CYAN + Style.BRIGHT + f'- Loading_{i}%.. ')
            if random.randint(1,4) < 3:
                time.sleep(random.random()/15)
        create_message_cube('-Loading Successfuly-',41,1)
        # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if  not os.path.isfile("Admin_name.json"): #如果没有Admin_name.json
            print(Fore.YELLOW + Style.BRIGHT + '[注册] - 输入您的名称... (Admin_name)')
            self.Admin_name = input()
            print(Fore.CYAN + Style.BRIGHT + f'{self.Admin_name},注册成功,加载文件')
            self.userlist.append(self.Admin_name)
            with open("Admin_name.json", "w") as f:
                json.dump(self.Admin_name, f)
                print(Fore.CYAN + Style.BRIGHT + "加载入文件完成...")
        elif os.path.isfile("Admin_name.json"):
            with open("Admin_name.json", "r") as f:
                self.Admin_name = json.load(f)
                if self.Admin_name not in self.userlist:
                    self.userlist.append(self.Admin_name)
                create_message_cube('检测到你的计算机已经有账号',41,1)
                print(Fore.CYAN + Style.BRIGHT + f"{self.Admin_name},欢迎登陆...")

        print(Fore.YELLOW + Style.BRIGHT +"[ Press'help'for more infomation ]")
        self.import_message()

    def import_message(self):
        self.Messageinput = input()
        if self.Messageinput == 'help':
            self.input_help()
        elif self.Messageinput == '/info':
            self.input_info()
        elif self.Messageinput[:4] == '/say':
            print(Fore.YELLOW + Style.BRIGHT + f":{self.Messageinput[4:]}")
        elif self.Messageinput == '/userlist':
            for i in range(len(self.userlist)):
                print(Fore.YELLOW + Style.BRIGHT + f'{self.userlist[i]}')
        elif self.Messageinput[:6] == '/admin':
            if str(self.Messageinput[6:]) not in self.userlist:
                print(Fore.YELLOW + Style.BRIGHT + f'{self.Messageinput[6:]} Not a user.')
        elif self.Messageinput[:8] == '/deadmin':
            if str(self.Messageinput[8:]) not in self.userlist:
                print(Fore.YELLOW + Style.BRIGHT +f'{self.Messageinput[8:]} Not Admin.')
        elif self.Messageinput[:4] == '/ban':
            if str(self.Messageinput[4:]) not in self.userlist:
                print(Fore.YELLOW + Style.BRIGHT +f'{self.Messageinput[4:]} Not a user.')
        elif self.Messageinput == '/reload':
            Server_say()
        elif self.Messageinput == '/deid':
            if os.path.exists('Admin_name.json'):
                os.remove('Admin_name.json')
                Server_say()
            else:
                print('no such file:%s' % my_file)
        else:
            print(f'{self.Admin_name} >>> :', self.Messageinput)
            print(Fore.RED + Style.BRIGHT +"无效命令 Press'help'for more infomation")

        self.import_message()

    def input_help(self):
        create_message_cube('- 帮助 -',42,1)
        print(Fore.MAGENTA + Style.BRIGHT + "- /admin + id 为设置管理命令")
        print(Fore.MAGENTA + Style.BRIGHT + "- /deadmin + id 取消管理命令")
        print(Fore.MAGENTA + Style.BRIGHT + "- /info 显示更多信息")
        print(Fore.MAGENTA + Style.BRIGHT + "- /say + 内容 Server内说话")
        print(Fore.MAGENTA + Style.BRIGHT + "- /userlist 查看当前用户列表")
        print(Fore.MAGENTA + Style.BRIGHT + "- /ban + id 封禁一个用户")
        print(Fore.MAGENTA + Style.BRIGHT + "- /reload 重新加载程序")
        print(Fore.MAGENTA + Style.BRIGHT + "- /deid 删除自己的id(重新注册)")
        print(Fore.GREEN + Style.BRIGHT + "-"*31)

    def input_info(self):
        print(Fore.YELLOW + Style.BRIGHT + "- 版权所有:PTG_STUDIO © ")
        print(Fore.YELLOW + Style.BRIGHT + "- 持有者: HFYHTVS ")
        print(Fore.YELLOW + Style.BRIGHT + "- infomation WorkStationRGS ")
        print(Fore.YELLOW + Style.BRIGHT + "- T喵 协议 ")

if __name__ == '__main__':
    init(autoreset=True)  # 初始化,并且设置颜色设置自动恢复
    Server_say()
        # 如果未设置autoreset=True,需要使用如下代码重置终端颜色为初始设置
            #print(Fore.RESET + Back.RESET + Style.RESET_ALL)  autoreset=True

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
HFYHTVSHFYHTVS

Tips:需要下载

colorama

点赞0


评论


HFYHTVSHFYHTVS

from colorama import *

#只能这样导入哦,记得后面的 if __name__ == '__main__':里面的初始化操作哦

点赞0


评论


酒精灯火焰酒精灯火焰

这是啥,干嘛的,有什么用???????????????????????上来就看了个婆娘

点赞0


评论


Little_ProgramLittle_Program

ddd(虽然看不懂)emotion_doge

点赞1


评论


真_吹神求杀真_吹神求杀

【非官方作业1】如果不喜欢colorama的话,也有名叫sheen的库可以选择,将至少3个运用colorama的函数替换为sheen。

【非官方作业2】开发简单的H喵L格式前端网页。(注:可以和服务器不发生交互,即静态网页)

点赞0


评论


玄离俊秀的狼王玄离俊秀的狼王

看到sys和os就知道是同道中人

点赞0


评论


DL1DL1

咋对接

点赞0


评论


乐观的超能鸭E2Al乐观的超能鸭E2Al

非常好,但是有一点瑕疵

1.“这个就算是框架了,对接你们自己搞,类和输入端我都做好了”

2.什么意思啊???有注释=没注释

点赞0


评论