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


Python Error.bad_request方法代码示例

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


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

示例1: __init__

# 需要导入模块: from error import Error [as 别名]
# 或者: from error.Error import bad_request [as 别名]
class Authentication:
    """
    Nhóm chức năng quản lý người dùng
    Các hành động hỗ trợ:
        * đăng ký
        * đăng nhập
        * đăng xuất
    """

    def __init__(self):
        # Connect to Cassandra servers
        client = connect(cassandra_hosts)
        self.a = ColumnFamily(client, cassandra_keyspace, "Users", super=True)
        self.e = Error()

    def _check(self, username):
        try:
            username = username.strip().lower()
            self.a.get(username)
            return 1
        except (NotFoundException, InvalidRequestException):
            return 0

    def _remove(self, username):
        try:
            username = username.strip().lower()
            self.a.remove(username)
            return 1
        except (NotFoundException, InvalidRequestException):
            return 0

    def _register(self, username, password, phone_number):
        try:
            username = username.strip().lower()
            if self._check(username) == 1:
                return False
            salt = str(random())
            password = sha512(password + salt).hexdigest()
            self.a.insert(
                username,
                {
                    "info": {
                        "password": password,
                        "salt": salt,
                        "phone_number": str(phone_number),
                        "join_time": str(datetime.now()),
                    }
                },
            )
            return True
        except (NotFoundException, InvalidRequestException):
            return False

    def _login(self, username, password):
        try:
            username = username.strip().lower()
            store_data = self.a.get(username, super_column="info")
            store_passwd = store_data["password"]
            store_salt = store_data["salt"]
            store_join_time = datetime.strptime(store_data["join_time"], "%Y-%m-%d %H:%M:%S.%f")
            password = sha512(password + store_salt).hexdigest()
            if password == store_passwd:
                session_id = md5(username + str(datetime.now())).hexdigest()
                # --- check trial expire ---
                delta = datetime.now() - store_join_time
                if delta.days > trial_time:
                    return 402
                # --- end check ---
                self.a.insert("session_id", {session_id: {"username": username, "timestamp": str(datetime.now())}})
                return session_id
            return False
        except (NotFoundException, InvalidRequestException):
            return False

    def _logout(self, session_id):
        try:
            self.a.get("session_id", super_column=session_id)  # check if exist
            self.a.remove("session_id", session_id)
            return True
        except (NotFoundException, InvalidRequestException):
            return False

    def register(self, environ):
        try:
            username = environ["request"]["username"]
            password = environ["request"]["password"]
            phone_number = environ["request"]["phone_number"]
        except KeyError:
            return self.e.bad_request("Hành động này yêu cầu 4 tham số: username, password, phone_number và api_key")
        username = username.strip().lower()
        # check params
        allow_chars = lowercase + "[email protected]"
        for char in username:
            if char not in allow_chars:
                self.e.param_error("Tên đăng nhập chỉ được dùng các ký tự a-z, A-Z và [email protected]")
        if len(password) != 32:
            return self.e.param_error("Mật khẩu phải được mã hóa md5 trước khi gửi lên")

        # ----- check phone_number (dạng 841673450799) -----
        if phone_number != "":
#.........这里部分代码省略.........
开发者ID:AloneRoad,项目名称:MELS,代码行数:103,代码来源:authentication.py


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