Exemplo n.º 1
0
def sendEmail(self,emailAdd,content,content_type = 0):

    msgRoot = MIMEMultipart('related')
    msgRoot['From'] = self.localVariable["__MAIL__"]
    msgRoot['To'] = "".join(emailAdd)
    subject = '验证码'
    msgRoot['Subject'] = Header(subject, 'utf-8')

    mail_msg = "<p>欢迎注册,您的验证码为: {code}</p>".format(code=content)

    msgRoot.attach(MIMEText(mail_msg, 'html', 'utf-8'))

    mailserver = "smtp.qq.com"            # 账号 qq 邮箱
    port = 465
    try:
        smtpObj = smtplib.SMTP_SSL(mailserver, port)
        # smtpObj = smtplib.SMTP(mailserver, port)
        sender = self.localVariable["__MAIL__"]
        passwd = self.localVariable["__MAIL_PASSWORD__"]
        smtpObj.login(sender, passwd)
        # smtpObj.set_debuglevel(1)
        # smtpObj.docmd("EHLO server")
        # smtpObj.docmd("AUTH LOGIN")
        smtpObj.sendmail(sender, emailAdd, msgRoot.as_string())
        self.write(json.dumps({"code": content}))
        weblog.info("邮件发送成功。", self._request_summary())
    except smtplib.SMTPException as e:
        weblog.info("Error: 无法发送邮件", self._request_summary(), e)
Exemplo n.º 2
0
 def get(self):
     weblog.info("%s.", self._request_summary())
     # return self.render('bug/bugtrackadd.html')
     return self.render('bug/bugtrackadd.html',
                        message="",
                        users=get_user_list(self),
                        projects=get_project_list(self))
Exemplo n.º 3
0
 def get(self):
     weblog.info("%s.", self._request_summary())
     task = dict()
     task['progress'] = 45
     self.render("project/task.html", message="", projects=get_project_list(self),
                 users=get_user_list(self), task=task)
     pass
Exemplo n.º 4
0
 def post(self, id):
     weblog.info("%s.", self._request_summary())
     username = self.get_argument("username", None)
     passowrd = self.get_argument("passowrd", None)
     useremail = self.get_argument("useremail", None)
     userrole = self.get_argument("userrole")
     msg = []
     if username is None or username == "":
         msg.append(msg_define.USERNAME_IS_EMPTY)
     if passowrd is None or check_passord(passowrd) is None:
         msg.append(msg_define.USERPASSWORD_INVALID)
     if useremail is None or useremail == "":
         msg.append(msg_define.USEREMAIL_IS_EMPTY)
     elif check_email(useremail) is None:
         msg.append(msg_define.USEREMAIL_INVALID)
     if msg:
         return self.render('admin/useredit.html', message=msg)
     else:
         try:
             old_user = get_user_by_id(self, id)
             old_user.username = username
             old_user.password = passowrd
             old_user.email = useremail
             old_user.userrole = userrole
             self.mysqldb().commit()
             return self.redirect('/user/list')
         except:
             weblog.exception("Edit user error!")
             self.mysqldb().rollback()
             return self.render('admin/useredit.html', message=msg)
Exemplo n.º 5
0
 def post(self, id):
     weblog.info("%s.", self._request_summary())
     Projectname = self.get_argument("Projectname", None)
     passowrd = self.get_argument("passowrd", None)
     Projectemail = self.get_argument("Projectemail", None)
     Projectrole = self.get_argument("Projectrole")
     msg = []
     if Projectname is None or Projectname == "":
         msg.append(msg_define.ProjectNAME_IS_EMPTY)
     if passowrd is None or check_passord(passowrd) is None:
         msg.append(msg_define.ProjectPASSWORD_INVALID)
     if Projectemail is None or Projectemail == "":
         msg.append(msg_define.ProjectEMAIL_IS_EMPTY)
     elif check_email(Projectemail) is None:
         msg.append(msg_define.ProjectEMAIL_INVALID)
     if msg:
         return self.render('admin/Projectedit.html', message=msg)
     else:
         try:
             old_Project = get_project_by_id(self, id)
             old_Project.Projectname = Projectname
             old_Project.password = passowrd
             old_Project.email = Projectemail
             old_Project.Projectrole = Projectrole
             self.mysqldb().commit()
             return self.redirect('/Project/list')
         except:
             weblog.exception("Edit Project error!")
             self.mysqldb().rollback()
             return self.render('admin/Projectedit.html', message=msg)
Exemplo n.º 6
0
 async def __save_secret(self, secret):
     key = 'vps-' + self.__remote_ip()
     redis = await aioredis.create_redis(self.redis_addr,
                                         timeout=REDIS_TIMEOUT)
     access_log.info('adding: ({}, {})'.format(key, secret))
     await redis.set(key, secret, expire=30)  # register secret
     redis.close()
Exemplo n.º 7
0
 def get(self, *args, **kwargs):
     weblog.info("%s. get extrabase.html", self._request_summary())
     topic_id = self.get_argument("topic_id", "0")
     topic = get_topic(self, topic_id)
     self.render("extra/topic.html",
                 topic=topic,
                 user=get_user_nickname(self))  # templates/
Exemplo n.º 8
0
 def get(self, *args, **kwargs):
     weblog.info("%s. get extrabase.html", self._request_summary())
     current_page = int(self.get_argument("current_page", "1"))
     topics, total_page = yield self.get_data(current_page)
     self.render("extrabase.html",
                 topics=topics,
                 user=get_user_nickname(self))  # templates/
Exemplo n.º 9
0
 def post(self):
     weblog.info("%s.", self._request_summary())
     cur_login_name = self.get_secure_cookie('user_account')
     # 用户是否存在
     cur_user_id = self.mysqldb().query(
         TblAccount.id, TblAccount.userstate).filter(
             TblAccount.username == cur_login_name).first()
     project_name = self.get_argument("project_name", None)
     top_project_id = self.get_argument("top_project_id", 0)
     project_describe = self.get_argument("project_describe", None)
     peers = self.get_arguments("peer")
     msg = ''
     try:
         new_project = TblProject()
         new_project.project_name = project_name
         new_project.progress = 0
         new_project.describe = project_describe
         new_project.status = msg_define.USER_NORMAL
         new_project.top_project_id = top_project_id
         new_project.created_by = cur_user_id.id
         self.mysqldb().add(new_project)
         self.mysqldb().commit()
         # 添加项目关联人员
         self.relation_project_user(project_name, peers)
         # return self.render('project/projectlist.html', projects=get_project_list(self),
         #                    project_map=get_project_map(self))
         return self.redirect('/project/list')
     except:
         weblog.exception("Add new Project error!")
         self.mysqldb().rollback()
         return self.render('project/projectadd.html',
                            message="",
                            users=get_user_list(self),
                            projects=get_project_list(self))
Exemplo n.º 10
0
 def get(self):
     weblog.info("%s.", self._request_summary())
     projects = get_project_list(self)
     project_map = get_project_map(self)
     return self.render('project/projectlist.html',
                        projects=projects,
                        project_map=project_map)
Exemplo n.º 11
0
 def post(self, code):
     email = self.get_argument('email')
     if not is_valid_email(email):
         error = 'Please enter a valid email address'
         self.render(template('auth/resend_verification.html'), error=error)
     yield send_verification_email(email)
     access_log.info('Sent email verification to %s' % email)
     self.render(template('auth/verification_sent.html'), error='')
Exemplo n.º 12
0
 def log_request(self, handler):
     super(DebuggingApplication, self).log_request(handler)
     size = self.connection_pool._queue.qsize()
     access_log.info(
         "Stats for Server on port '{}': Active Server connections: {}, DB Connections in pool: {}, DB Pool size: {}"
         .format(tornado.options.options.port,
                 len(self._server._connections), size,
                 self.connection_pool._con_count))
Exemplo n.º 13
0
 def get(self):
     weblog.info("%s , TableTestHandler.", self._request_summary())
     data = {"name": "GET", "value": "v12", "type": 1}
     datas = []
     datas.append(data)
     datas.append(data)
     # self.render('tabletest.html',data=datas)
     self.render('project/multiselect.html')
Exemplo n.º 14
0
 def request(self):
     handle = getattr(requests, self.dispatch())
     try:
         result = handle("%s:%s%s" % (self.host, self.port, self.url), params=self.params, data=self.data)
         result = json.loads(result.text)
     except Exception, e:
         log.info("request the url: {} failed for this reason {}".format(self.url, e))
         return False, None
Exemplo n.º 15
0
 def get(self, bid):
     weblog.info("%s.", self._request_summary())
     # return self.render('bug/bugtracklist.html', bugtracks=get_bugtrack_list(self, belongid, compid))
     return self.render('bug/bugtrackedit.html',
                        message="",
                        users=get_user_list(self),
                        projects=get_project_list(self),
                        bug=get_bugtrack_by_id(self, bid))
Exemplo n.º 16
0
 def get(self, *args, **kwargs):
     weblog.info("%s.", self._request_summary())
     data = yield self.get_data()
     # print(data)
     self.render("extra/setting.html",
                 user=get_user_nickname(self),
                 data=data)
     pass
Exemplo n.º 17
0
 async def __load_secret(self):
     key = 'vps-' + self.__remote_ip()
     redis = await aioredis.create_redis(self.redis_addr,
                                         timeout=REDIS_TIMEOUT)
     secret = await redis.get(key)
     redis.close()
     access_log.info('retrieving: ({}, {})'.format(key, secret))
     return secret
Exemplo n.º 18
0
    def update_client(self, connection, msg, authorization, client):
        client.update_configuration(clean_session=msg.clean_session,
                                    keep_alive=msg.keep_alive)
        client.update_connection(connection)
        client.update_authorization(authorization)

        access_log.info("[uid: %s] Reconfigured client upon "
                        "reconnection." % client.uid)
Exemplo n.º 19
0
 def open(self):
     key = self.get_query_argument('key', '')
     if key != app_key:
         access_log.warn('Access denied on %s.' % self.request.remote_ip)
         self.write_message("Access denied")
         self.close()
     else:
         self.write_message("Access granted")
         access_log.info('Access allowed on %s.' % self.request.remote_ip)
Exemplo n.º 20
0
Arquivo: base.py Projeto: albzcj/blog
 def prepare(self):
     #根据cookies生成用户信息
     access_log.info('>>>>>>>>>>>>>>>>>>>>>>>')
     username = self.get_cookie('username','')
     uinfo = db.con.user.find_one({'username':username})
    # print username
     self.uinfo = uinfo
     if username:
         pass
Exemplo n.º 21
0
 def delete(self, uid):
     weblog.info("uid={}".format(uid))
     try:
         self.mysqldb().query(TblAccount).filter(TblAccount.id == uid).delete()
         self.mysqldb().commit()
         return self.write(json_dumps({"error_code": 0, "msg": u"删除成功"}))
     except Exception as e:
         weblog.error("{}".format(e))
         return self.write(json_dumps({"error_code": 1, "msg": u"删除失败。{}".format(e)}))
Exemplo n.º 22
0
 def get(self):
     print(datetime.now(), "start")
     # self.render("testclick.html")
     weblog.info("%s.", self._request_summary())
     weblog.info("get home")
     result = yield self.doing()
     print(result)
     # self.render("admin/homepage.html")
     self.write(self.render_template('admin/homepage.html'))
Exemplo n.º 23
0
 def prepare(self):
     self._start_time = time.time()
     arguments = self.request.arguments
     if self.current_user is None:
         user = "******"
     else:
         user = self.current_user["email"]
     access_log.info("uri: %s, method: %s, user: %s, arguments: %s, ip: %s" % (
             self.request.uri, self.request.method, user,
             arguments, self.request.remote_ip))
Exemplo n.º 24
0
    def post(self, *args, **kwargs):
        weblog.info("%s. post add a topic", self._request_summary())
        title = self.get_argument("title", None)
        content = self.get_argument("content", None)
        category = int(self.get_argument("category", "0"))

        if title is None or content is None:
            return self.write(json_dumps({"msg": u"标题或内容不能为空"}))

        yield self.add_topic(title, content, category)
Exemplo n.º 25
0
 def get(self):
     current_page = int(self.get_argument("current_page", FIRST_PAGE))
     # users = get_user_list(self)
     users, total_page = get_user_pagination(self, current_page)
     weblog.info("{2}. user list total page:{0},current page:{1}".format(
         total_page, current_page, self._request_summary()))
     return self.render('admin/usermanage.html',
                        users=users,
                        current_page=current_page,
                        total_page=total_page)
Exemplo n.º 26
0
 def get(self):
     belongid = self.get_argument("belongid", None)
     compid = self.get_argument("compid", None)
     blevel = self.get_argument("blevel", None)
     weblog.info("%s.", self._request_summary())
     return self.render('bug/bugtracklist.html',
                        bugtracks=get_bugtrack_list(self, belongid, compid,
                                                    blevel),
                        users=get_user_list(self),
                        projects=get_project_list(self))
Exemplo n.º 27
0
    def put(self):
        weblog.info("%s. put edit a topic", self._request_summary())
        title = self.get_argument("title", None)
        content = self.get_argument("content", None)
        category = int(self.get_argument("category", "0"))
        topic_id = self.get_argument("topic_id", "0")

        if title is None or content is None:
            return self.write(json_dumps({"msg": u"标题或内容不能为空"}))

        yield self.edit_topic(title, content, category, topic_id)
Exemplo n.º 28
0
 def get(self, cid):
     weblog.info("%s.", self._request_summary())
     comp_mt = self.mysqldb().query(TblComponentMaintenance).filter_by(comp_id=cid).first()
     comp_mt.comp_status = -1
     try:
         self.mysqldb().commit()
     except Exception as e:
         weblog.error("delete component maintenance id={} error. {}".format(cid, e))
         self.mysqldb().rollback()
     return self.render('bug/compmtlist.html', bugs=get_compmt_list(self),
                        users=get_user_list(self), projects=get_project_list(self))
Exemplo n.º 29
0
 def get(self, pjid):
     weblog.info("%s. pjid=%d", self._request_summary(), pjid)
     project = self.mysqldb().query(TblProject).filter_by(
         project_id=pjid).first()  # u"必须查询对象"
     # print(project)
     try:
         project.status = 1
         self.mysqldb().commit()
     except Exception as e:
         weblog.error("delete project id={} fail:{}.".format(pjid, e))
         self.mysqldb().rollback()
     return self.redirect('/project/list')
Exemplo n.º 30
0
 def open(self):
     # returns the value of the argument with the given name from the request
     # query string (the url)
     # it's the key that will be compared with the one saved
     key = self.get_query_argument('key', '')
     if key != app_key:
         access_log.warn('Access denied on %s.' % self.request.remote_ip)
         self.write_message("Access denied")
         self.close()
     else:
         self.write_message("Access granted")
         access_log.info('Access allowed on %s.' % self.request.remote_ip)
Exemplo n.º 31
0
    def get(self, bid):
        weblog.info("%s.", self._request_summary())
        bug_track = self.mysqldb().query(TblBugTrack).filter_by(
            comp_id=bid).first()
        bug_track.bug_project_id = -1
        try:
            self.mysqldb().commit()
        except Exception as e:
            weblog.error("delete bug track id={} error. {}".format(bid, e))
            self.mysqldb().rollback()

        return self.render('bug/bugtracklist.html',
                           bugtracks=get_bugtrack_list(self))
Exemplo n.º 32
0
 def post(self):
     destr = self.get_argument("destr", "")
     bstr = destr.encode('utf-8')
     # print(bstr)
     error_code = 0
     weblog.info("destr:{}".format(bstr))
     try:
         result = self_decode(bstr)
     except Exception as e:
         weblog.error("{}".format(e))
         result = u"解密失败"
         error_code = 1
     return self.write(json_dumps({"decode": result, "error_code": error_code}))
Exemplo n.º 33
0
 def get(self, *args, **kwargs):
     """
     get_user_nickname  and self.current_user
     :param args:
     :param kwargs:
     :return:
     """
     weblog.info("%s. get topicadmin.html", self._request_summary())
     current_page = int(self.get_argument("current_page", "1"))
     topics, total_page = yield self.get_data(current_page)
     self.render("extra/topicadmin.html",
                 topics=topics,
                 user=get_user_nickname(self))  # templates/
Exemplo n.º 34
0
    def _make_proxy_request(self, request_data):
        timeout = float(request_data.get("timeout", DEFAULT_TIMEOUT))
        validate_cert = bool(request_data.get("validate_cert") or True)
        max_redirects = request_data.get("max_http_redirects") or 0
        follow_redirects = max_redirects > 0  # 0 means do not follow redirects

        url = request_data.get("url")
        params = request_data.get("data")
        post_type = request_data.get("post_type")
        if params and post_type is None:
            url = "%s?%s" % (url, urlencode(params))

        logger.info("[%s]agent request url: %s", self.id, url)

        proxy_request = HTTPRequest(
            url, validate_cert=validate_cert,
            headers=self._get_proxy_request_headers(request_data),
            method=request_data.get("method", "GET"),
            allow_nonstandard_methods=True,
            connect_timeout=timeout,
            request_timeout=timeout,
            streaming_callback=self._streaming_callback,
            header_callback=self._header_callback,
            follow_redirects=follow_redirects,
            max_redirects=max_redirects,
            prepare_curl_callback=self.prepare_curl_callback,
        )

        role_name = request_data.get("role")
        if role_name:
            InterfaceRoleManager.set_curl_interface_role(
                proxy_request, role_name,
            )

        keystone_auth_info = request_data.get("keystone")
        if keystone_auth_info:
            logger.warning(
                "[%s]agent request required keystone token",
            )
            auth_headers = yield self._get_keystone_auth_headers(
                keystone_auth_info, validate_cert=validate_cert,
            )
            if not auth_headers:
                raise gen.Return()
            proxy_request.headers.update(auth_headers)

        body = self._get_request_body(request_data)
        if body:
            proxy_request.body = body

        raise gen.Return(proxy_request)
Exemplo n.º 35
0
    def post(self):
        weblog.info("%s.", self._request_summary())
        bug_component = int(self.get_argument("bug_component", '-1'))
        component_belong = int(self.get_argument("component_belong", '-1'))
        bug_level = int(self.get_argument("bug_level", '-1'))
        bug_component = None if bug_component < 0 else bug_component
        component_belong = None if component_belong < 0 else component_belong
        bug_level = None if bug_level < 0 else bug_level
        bugtracks = get_bugtrack_list(self, component_belong, bug_component,
                                      bug_level)
        weblog.info("%s. %d %d %d", self._request_summary(), bug_component,
                    component_belong, bug_level)

        return self.write(json.dumps(bugtracks))
Exemplo n.º 36
0
    def get(self, code):
        try:
            email = check_signature(code, 86400)  # 24 hours
        except:
            error = 'Signature %s did not authenticate.' % code
            access_log.error(error)
            self.render(template('auth/signature_invalid.html'), error='')

        email = str(email)[2:-1]
        yield activate_user(self.db, email)
        access_log.info('%s email verified, \
                        user account activated and logged in.' % email)
        self.set_current_user(email)
        self.redirect('/')
Exemplo n.º 37
0
    def prepare(self):
        try:
            user_agent = self.request.headers['User-Agent']
        except KeyError:
            user_agent = '-'

        access_log.info('request log -> %s|%s|%s|%s' % (
            self.request.remote_ip,
            self.request.method,
            self.request.uri,
            user_agent)
        )

        if not self.settings['admin'] and self.request.method == 'GET' and self.request.uri != '/welcome':
            self.redirect('/welcome')
Exemplo n.º 38
0
    def create_client(self, connection, msg, authorization):
        client_persistence = self.persistence.get_for_client(msg.client_uid)
        client = MQTTClient(
            server=self,
            connection=connection,
            authorization=authorization,
            uid=msg.client_uid,
            clean_session=msg.clean_session,
            keep_alive=msg.keep_alive,
            persistence=client_persistence,
        )

        # verbosity... testing
        type = "plain-client"
        if client.is_broker():
            type = "broker"
        access_log.info("[uid: %s] new session created (client type: %s)" % (client.uid, type))
        return client
Exemplo n.º 39
0
    def remove_client(self, client):
        """
        Removes a client from the know clients list. It's safe to call this
        method without checking if the client is already known.

        :param MQTTClient client: A :class:`broker.client.MQTTClient` instance;

        .. caution::
           It won't force client disconnection during the process, which can
           result in a lingering client in  the Tornado loop.
        """
        assert isinstance(client, MQTTClient)

        self.persistence.remove_client_data(client.uid)

        if client.uid in self.clients:
            del self.clients[client.uid]
            access_log.info("[uid: %s] session cleaned" % client.uid)
Exemplo n.º 40
0
    def post(self):
        email = self.get_argument('email')
        password = self.get_argument('password')
        if not is_valid_email(email):
            error = 'Please enter a valid email address'
            self.render(template('auth/register.html'), error=error)

        yield send_verification_email(email)

        rdb = yield add_user(self.db, email, password)
        if rdb.get('first_error') is None:
            access_log.info('%s registered and \
                            verification email sent.' % email)
            self.render(template('auth/verify_email.html'))
        else:
            access_log.error(rdb.get('first_error'))
            error = 'An error occurred adding user to the database.'
            self.render(template('auth/register.html'), error=error)
Exemplo n.º 41
0
def load_app(port, root):
    settings = {
        "socket_io_port": port,
        "static_path": path.join(root, "client"),
        "template_path": path.join(root, "client"),
        "debug" : True,
        "xsrf_cookies": False,
    }
    
    routers = load_routers()

    application = tornado.web.Application(
        routers,
        **settings
    )
    access_log.info("Starting server")
    application.listen(port)
    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 42
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type == toro.Timeout:
            access_log.debug("[uid: %s] connection timeout" % self.client_uid)

        elif exc_type == StreamClosedError:
            access_log.warning("[uid: %s] stream closed unexpectedly" % self.client_uid)

        elif exc_type == ConnectError:
            self.stream.close()
            access_log.info("[uid: %s] connection refused: %s" % (self.client_uid, exc_val.message))

        elif exc_type == Exception:
            access_log.exception("[uid: %s] error handling stream" % self.client_uid, exc_info=True)

        if exc_val is not None:
            if self.client is not None:
                self.client.disconnect()

        return True  # suppress the raised exception
Exemplo n.º 43
0
    def post(self):
        old_pass = self.get_argument('old_password')
        new_pass = self.get_argument('new_password')
        new_pass_verify = self.get_argument('new_password1')
        email = self.get_current_user()
        if new_pass != new_pass_verify:
            error = 'New passwords must match'
            self.render(template('auth/change_password.html'), error=error)

        auth = yield verify_user(self.db, email, old_pass)
        if auth:
            change_password(self.db, email, new_pass)
            yield send_password_changed_email(email)
            self.redirect('/')
        else:
            access_log.info('Change password requested by %s, \
                            but wrong old password' % email)
            error = 'Wrong password, try again?'
            self.render(template('auth/change_password.html'), error=error)
Exemplo n.º 44
0
    def _fetch_proxy_request(self, proxy_request):
        self.in_request_headers = False
        try:
            response = yield self.http_client.fetch(proxy_request)
        except HTTPError as err:
            self.set_status(err.code, err.message)
            raise gen.Return()
        except Exception as err:
            self.set_status(503, str(err))
            raise gen.Return()

        if response.error:
            self.set_status(response.code, str(response.error))
        else:
            self.set_status(response.code, response.reason)

        logger.info(
            "[%s]agent response status: %s, reason: %s",
            self.id, response.code, response.reason,
        )
Exemplo n.º 45
0
    def _get_keystone_auth_headers(self, auth_info, validate_cert=True):
        try:
            response = yield self.http_client.fetch(
                auth_info.get("auth_url"), method="POST",
                headers={"Content-Type": "application/json"},
                validate_cert=validate_cert,
                body=json.dumps({
                    "auth": {
                        "passwordCredentials": {
                            "username": auth_info.get("user_name"),
                            "password": auth_info.get("password"),
                        },
                        "tenantName": auth_info.get("tenant_name"),
                    }
                })
            )
        except Exception as err:
            logger.info(err)
            self.set_status(503, "keystone auth error")
            raise gen.Return()

        if response.error or response.code != 200:
            logger.info("keystone auth error")
            self.set_status(407, "keystone auth error")
            raise gen.Return()

        auth_info = json.loads(response.body.decode("utf-8"))
        try:
            raise gen.Return({
                "X-AUTH-TOKEN": auth_info["access"]["token"]["id"],
            })
        except KeyError:
            logger.info("keystone auth failed")
            self.set_status(407, "keystone auth failed")
        raise gen.Return()
Exemplo n.º 46
0
    def prepare(self):
        try:
            user_agent = self.request.headers['User-Agent']
        except KeyError:
            user_agent = '-'

        access_log.info('request log -> %s|%s|%s|%s' % (
            self.request.remote_ip,
            self.request.method,
            self.request.uri,
            user_agent)
        )

        session_id = self.get_cookie('s')

        if not session_id:
            session_id = str(uuid.uuid4())
            # Set session
            self.set_cookie(name="s", value=session_id)

        self.current_user = self.get_current_user()

        if not self.current_user:
            self.session.record_visitor(session_id, 'Anonymous', self.request.remote_ip, user_agent)
            session_check = self.session.check_visitor(session_id)

            if session_check['requests'] > 5:
                self.set_status(403)
                self.finish('Access Denied')
                return

        # if the site is just created without a admin user
        if not self.settings['admin']:
            if self.request.uri.startswith('/api'):
                self.set_status(403)
                self.finish('login first')

            elif self.request.uri != '/welcome':
                self.redirect('/welcome')
Exemplo n.º 47
0
    def forward_subscription(self, topic, granted_qos, sender_uid):
        """
        :param topic_qos: A list of topic-qos-pairs to forward.
        :param sender_uid: sender's ID, don't send subscription back there!
        :return: FIXME (not specified yet)
        """
        """
        if not (topic, granted_qos) in self.hacked_topic_list:
            access_log.info("[.....] forwarding subscription from: \"%s\"" % sender_uid)
            # TODO rebuild package with subscription intents; think about qos
            msg = Subscribe()

            # recipients = [c for c in self.clients if c.receive_subscriptions and not c.uid == sender_uid]
            recipients = filter(lambda client: client.receive_subscriptions and not client.uid == sender_uid,
                            self.clients.values())
            for client in recipients:
                client.send_packet(msg)
            # TODO send it to "uplink" message broker if it did not come from there
            if sender_uid:  # XXX uplink is not a client, assume sender_uid would be None
                pass
                # Uplink.send_packet(msg)
        """
        access_log.info('[.....] forwarding subscription from: "%s"' % sender_uid)

        # TODO rebuild package with subscription intents; think about qos
        msg = Subscribe()

        # recipients = [c for c in self.clients if c.receive_subscriptions and not c.uid == sender_uid]
        recipients = filter(
            lambda client: client.receive_subscriptions and not client.uid == sender_uid, self.clients.values()
        )
        for client in recipients:
            client.send_packet(msg)

        # TODO send it to "uplink" message broker if it did not come from there
        if sender_uid:  # XXX uplink is not a client, assume sender_uid would be None
            pass
Exemplo n.º 48
0
 def post(self):
     email = self.get_argument("email")
     password = self.get_argument("password")
     auth = yield verify_user(self.db, email, password)
     if auth:
         activated = yield is_activated(self.db, email)
         if activated:
             access_log.info('%s logged in.' % email)
             self.set_current_user(email)
             self.redirect(self.get_argument("next", u"/"))
         else:
             access_log.info('%s attempted login, \
                             but email not verified, \
                             re-sending verification email.' % email)
             yield send_verification_email(email)
             self.render(template("auth/verify_email.html"))
     else:
         access_log.info('%s failed login.' % email)
         self.render(template("auth/login.html"),
                     error="Login incorrect",
                     user=self.get_current_user())
Exemplo n.º 49
0
 def on_finish(self):
     cost_time = time.time() - self._start_time
     access_log.info("cost time: %0.3f" % (cost_time, ))
Exemplo n.º 50
0
def setup_loggers ():
	"""
	set up tornado loggers with custom format
	
	logger has 5 severity levels : 
		D - DEBUG (lowest)
		I - INFO
		W - WARNING
		E - ERROR
		C - CRITICAL (highest)
	"""

	# config logger output in console
	# logging.basicConfig(	level 	= logging.DEBUG, 
	# 						format 	= "%(name)s - %(funcName)s - %(levelname)s : %(message)s" )

	# Create a Formatter for formatting the log messages
	# log_formatter = logging.Formatter('%(name)s -- %(funcName)s - %(levelname)s - %(message)s')
	openscraper_log_format = '%(color)s::: %(levelname)s %(name)s %(asctime)s ::: %(module)s:%(lineno)d -in- %(funcName)s() :::%(end_color)s \
		%(message)s' 
	# datefmt='%y%m%d %H:%M:%S'
	# style='%'
	# color=True
	# colors={40: 1, 10: 4, 20: 2, 30: 3}
	"""	
	default tornado format for logging : 
		fmt='%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s'
		datefmt='%y%m%d %H:%M:%S'
		style='%'
		color=True
		colors={40: 1, 10: 4, 20: 2, 30: 3}
		))
	"""
	# log_formatter = logging.Formatter( fmt=openscraper_log_format )
	tornado_log_formatter = LogFormatter(fmt=openscraper_log_format, color=True)

	enable_pretty_logging()

	### Logger as self var
	# create the Logger
	# dictConfig(logging_config)
	# self.log 		= logging.getLogger(__name__)
	# self.logger 	= logging.getLogger()
	# self.access_log = logging.getLogger("tornado.access")
	# self.app_log 	= logging.getLogger("tornado.application")
	# self.gen_log 	= logging.getLogger("tornado.general")

	### Get root logger
	root_logger 	= logging.getLogger()
	# print root_logger.__dict__

	### Format root_logger stream
	# parent_logger = app_log.parent
	# print parent_logger.__dict__
	# root_stream_handler = parent_logger.handlers
	# root_stream_handler[0].setFormatter(tornado_log_formatter)
	root_logger.handlers[0].setFormatter(tornado_log_formatter)

	# streamHandler 	= logging.StreamHandler() # stream=sys.stdout
	# streamHandler.setFormatter(tornado_log_formatter)
	# self.gen_log.addHandler(streamHandler)
	# self.app_log.addHandler(streamHandler)
	# self.access_log.addHandler(streamHandler)

	# self.log.setLevel(logging.DEBUG)



	# Create the Handlers for logging data to log files
	gen_log_handler 	= logging.FileHandler('logs/openscraper_general.log')
	gen_log_handler.setLevel(logging.WARNING)

	access_log_handler 	= logging.FileHandler('logs/openscraper_access.log')
	access_log_handler.setLevel(logging.WARNING)

	app_log_handler 	= logging.FileHandler('logs/openscraper_app.log')
	app_log_handler.setLevel(logging.WARNING)

	# Add the Formatter to the Handler
	gen_log_handler.setFormatter(tornado_log_formatter)
	access_log_handler.setFormatter(tornado_log_formatter)
	app_log_handler.setFormatter(tornado_log_formatter)


	# Add the Handler to the Logger
	gen_log.addHandler(gen_log_handler)
	access_log.addHandler(access_log_handler)	
	app_log.addHandler(app_log_handler)	
	
	# test loggers
	print()
	app_log.info('>>> this is app_log ')
	gen_log.info('>>> this is gen_log ')
	access_log.info('>>> this is access_log ')
	print()
Exemplo n.º 51
0
 def log_request(self, handler):
     request_time = 1000.0 * handler.request.request_time()
     access_log.info("%d %s %.2fms", handler.get_status(),
                handler._request_summary(), request_time)
Exemplo n.º 52
0
 def recreate_sessions(self, uids):
     access_log.info("recreating %s sessions" % len(uids))
     for uid in uids:
         if uid not in self.clients:
             self.add_client(self.recreate_client(str(uid)))
Exemplo n.º 53
0
 def get(self):
     user = self.get_current_user()
     access_log.info('%s logged out.' % user)
     self.clear_cookie("user")
     self.redirect(self.get_argument("next", "/"))
Exemplo n.º 54
0
    allows_methods = ["get", "post"]

    def __init__(self, url, host="", method="get", params=None, data=None):
        self.host = host or DEFAULT_HOST
        self.port = host or DEFAULT_PORT
        self.url = url
        self.method = method
        self.params = params
        self.data = data

        # enable log
        enable_pretty_logging()

    def dispatch(self):
        method = self.method.lower()
        if method not in self.allows_methods:
            raise TypeError("Method must be get or post")
        return method

    def request(self):
        handle = getattr(requests, self.dispatch())
        try:
            result = handle("%s:%s%s" % (self.host, self.port, self.url), params=self.params, data=self.data)
            result = json.loads(result.text)
        except Exception, e:
            log.info("request the url: {} failed for this reason {}".format(self.url, e))
            return False, None
        else:
            log.info(json.dumps(result, indent=4, ensure_ascii=False))
            return True, result
Exemplo n.º 55
0
 def prepare(self):
     """Log access."""
     request_time = 1000.0 * self.request.request_time()
     access_log.info(
         "%d %s %.2fms", self.get_status(),
         self._request_summary(), request_time)
Exemplo n.º 56
0
 def on_close(self):
     access_log.info('Client %s disconnected.' % self.request.remote_ip)
Exemplo n.º 57
0
    def update_client(self, connection, msg, authorization, client):
        client.update_configuration(clean_session=msg.clean_session, keep_alive=msg.keep_alive)
        client.update_connection(connection)
        client.update_authorization(authorization)

        access_log.info("[uid: %s] Reconfigured client upon " "reconnection." % client.uid)
Exemplo n.º 58
0

class AuthLoginHandler(BaseHandler, tornado.auth.GoogleMixin):
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        if self.get_argument("openid.mode", None):
            user = yield self.get_authenticated_user()
            # Save the user with e.g. set_secure_cookie()
            self.set_secure_cookie("dmonitoring_user",
                    tornado.escape.json_encode(user),
                    expires_days=7)
            self.redirect(self.get_argument("next", "/"))
        else:
            yield self.authenticate_redirect()


class AuthLogoutHandler(BaseHandler):
    def get(self):
        self.clear_cookie("dmonitoring_user")
        self.redirect(self.get_argument("next", "/"))


if __name__ == "__main__":
    set_options()
    access_log.info("Server is starting on port %s" % options.port)
    app = Application()
    app.listen(options.port, xheaders=True)
    tornado.ioloop.IOLoop.instance().start()

Exemplo n.º 59
0
                "handlers": ["file"],
                "level": "DEBUG"
            }
        }
    })

    if options.interface_roles:
        InterfaceRoleManager.setup_roles(dict(
            i.split(":", 1)
            for i in options.interface_roles.split(",")
        ))

    application = web.Application([
        (r"/?", IndexHandler),
        (r"/request/?", ProxyHandler),
    ], debug=options.debug)

    if options.curl_httpclient:
        AsyncHTTPClient.configure(
            "tornado.curl_httpclient.CurlAsyncHTTPClient"
        )

    http_server = httpserver.HTTPServer(application)
    http_server.listen(options.port)
    instance = ioloop.IOLoop.instance()
    try:
        logger.info("server listen on %s", options.port)
        instance.start()
    except KeyboardInterrupt:
        logger.info("bye")