Пример #1
0
    def __init__(self):
        from d3status.urls import handlers, ui_modules
        from d3status.db import Model

        settings = dict(debug=options.debug,
                        template_path=os.path.join(os.path.dirname(__file__),
                                                   "templates"),
                        static_path=os.path.join(os.path.dirname(__file__),
                                                 "static"),
                        login_url=options.login_url,
                        xsrf_cookies=options.xsrf_cookies,
                        cookie_secret=options.cookie_secret,
                        ui_modules=ui_modules,
                        #autoescape=None,
                        )

        # d3status db connection
        self.db = Connection(host=options.mysql["host"] + ":" +
                                options.mysql["port"],
                             database=options.mysql["database"],
                             user=options.mysql["user"],
                             password=options.mysql["password"],
                             )

        Model.setup_dbs({"db": self.db})

        super(Application, self).__init__(handlers, **settings)
Пример #2
0
def do_query(database=None):
    # Pick up the database credentials
    app.logger.warning("%s requesting access to %s database" %
                       (request.remote_addr, database))
    creds = get_db_creds(database)

    # If we couldn't find corresponding credentials, throw a 404
    if creds == False:
        return {"ERROR": "Unable to find credentials matching %s." % database}
        abort(404)

    # Prepare the database connection
    app.logger.debug("Connecting to %s database (%s)" %
                     (database, request.remote_addr))
    db = Connection(**creds)

    # See if we received a query
    sql = request.form.get('sql')
    if not sql:
        return {"ERROR": "SQL query missing from request."}

    # If the query has a percent sign, we need to excape it
    if '%' in sql:
        sql = sql.replace('%', '%%')

    # Attempt to run the query
    try:
        app.logger.info("%s attempting to run \" %s \" against %s database" %
                        (request.remote_addr, sql, database))
        results = db.query(sql)
    except Exception, e:
        return {"ERROR": ": ".join(str(i) for i in e.args)}
Пример #3
0
    def db(self):
        # Todo: Get from config
        if not hasattr(self, "_db"):
            self._db_connection = Connection(host="localhost", \
                database="uploadr",\
                user="******")

        return self._db_connection
Пример #4
0
def connect():
    conn.mysql = Connection(
        host=options.mysql["host"] + ":" + options.mysql["port"],
        database=options.mysql["database"],
        user=options.mysql["user"],
        password=options.mysql["password"])

    # ping db periodically to avoid mysql go away
    PeriodicCallback(_ping_db, int(options.mysql["recycle"]) * 1000).start()
Пример #5
0
    def __init__(self, root, charset):
        self.root = root
        self.charset = charset
        self.user_agent = 'zfz-bot/1.0'
        self.link_pattern = re.compile(r'\s+href="([^\s\'">]+)"[\s>]',
                                       re.U | re.I)
        self.price_pattern = re.compile(
            ur'租(\s|&nbsp;)*金[^::]*[::]\s*(<[^<>]+>\s*)*(\d+)\s*(<[^<>]+>\s*)*元/月',
            re.U | re.I)
        self.area_pattern = re.compile(
            ur'(面(\s|&nbsp;)*积[::]\s*(<[^<>]+>\s*)*|室\s*|卫\s*|厅\s*)([\d\.]+)\s*(平米|㎡|平方米)',
            re.U | re.I)
        self.arch_pattern = re.compile(
            ur'[房户](\s|&nbsp;)*型[^::]*[::]\s*(<[^<>]+>\s*)*(\d[^<\s]+)[<\s]',
            re.U | re.I)
        self.title_pattern = re.compile(ur'<title>\s*([^<]+[^\s])\s*</title>',
                                        re.U | re.I)
        self.address_pattern = re.compile(
            ur'地(\s|&nbsp;)*址[::]\s*(<[^<>]+>\s*)*([^<>\s]+)[<\s]',
            re.U | re.I)
        self.district_pattern = re.compile(
            ur'(小(\s|&nbsp;)*区(名称)?|楼盘名称)[::]\s*(<[^<>]+>\s*)*([^<>\s]+)[<\s]',
            re.U | re.I)

        self.max_url_length = 200
        self.max_price_length = 10
        self.max_area_length = 10
        self.max_arch_length = 20
        self.max_title_length = 100
        self.max_address_length = 100
        self.max_district_length = 20

        self.db = Connection('127.0.0.1', 'zfz', 'zfz', 'zfz...891')
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(),
                                           urllib2.HTTPRedirectHandler())
        self.opener.addheaders = [('User-agent', self.user_agent)]

        self.rerp = robotexclusionrulesparser.RobotExclusionRulesParser()
        self.rerp.user_agent = self.user_agent
        try:
            self.rerp.fetch(self.root[:self.root.find('/', 7)] + "/robots.txt")
        except:
            pass

        self.min_delay_seconds = 120.0
        self.max_crawl_seconds_per_site = 2 * 24 * 3600  # 2 days

        self.max_allowed_urlopen_error = 20
        self.current_urlopen_error = 0

        self.debug = True
    def UserLogin(self, controller, request, done):
        #print request

        # Extract name from the message received
        name = request.user_name
        passwd = self.enc_pass(request.user_pass)
        ret = False
        db = Connection('localhost', 'xituan-thinkphp', 'root')
        try:
            query = "select count(*) as cnt from user_login where `user_email` = '%s' and `user_password` = '%s'" % (
                name, passwd)
            logging.debug(query)
            ret = db.get(query)
        except Exception, e:
            logging.error(e)
Пример #7
0
from tornado.database import Connection
from urlparse import urlparse
import sys
# This file is a script that setups the database for cucket if the tables don't already exist

#DATABASE_URL = sys.argv[1]
#url = urlparse(DATABASE_URL)

#db = Connection(host=url.hostname, user=url.username, password=url.password, database=url.path[1:])
db = Connection(host="engr-cpanel-mysql.engr.illinois.edu", user="******", password="******", database="cubucket_db")
#db = Connection(host='localhost:3306', user='******', password='', database='cucket')  # will later need to change this for heroku


# Drop the existing tables
"""Comment this out so that we don't lose data from now on
tables = ['UserCompleted', 'UserInterest', 'Category', 'Activity', 'User']
for table in tables:
    sql = "DROP TABLE IF EXISTS `{0}`".format(table)
    db.execute(sql)
"""


# Create the User table
sql = """CREATE TABLE IF NOT EXISTS User(\
    name varchar(15) NOT NULL PRIMARY KEY,\
    password varchar(100) NOT NULL\
);"""
db.execute(sql)

# Create the Activity table
sql = """CREATE TABLE IF NOT EXISTS Activity(\
Пример #8
0
from tornado.options import options
from tornado.database import Connection

from d3status.libs.options import parse_options

parse_options()

from d3status.db import Model
from d3status.db import load_model
from d3status.mail import send_email
from d3status.tasks import status_tasks

# db connection
db = Connection(
    host=options.mysql["host"] + ":" + options.mysql["port"],
    database=options.mysql["database"],
    user=options.mysql["user"],
    password=options.mysql["password"],
)

Model.setup_dbs({"db": db})


def update_server_status():
    url = options.d3_server_status_url
    req = HTTPRequest(url=url)

    client = HTTPClient()
    response = client.fetch(req)
    if response.code == 200:
        status = _parse_server_status(response.body)
        changed_status = load_model("status").update_status(status)
Пример #9
0
    prompt = """\

The script will now try to connect to...
    database:   '%s'
    on host:    '%s'
    using user: '******'

""" % (database, host, user)
    print prompt


host, db, user, password = get_db_credentials()
print_connection_prompt(db, host, user)
sure = raw_input('Are you sure? (yes/no) ')
if sure in ('yes', 'Yes', 'y', 'Y'):
    db = Connection(host=host, database=db, user=user, password=password)
else:
    print "Operation aborted."
    sys.exit(1)

# Drop existing tables
cmd = """\
    DROP TABLE IF EXISTS `RelatesTo`;\
    DROP TABLE IF EXISTS `SpecializesIn`;\
    DROP TABLE IF EXISTS `Interests`;\
    DROP TABLE IF EXISTS `FitnessTopics`;\
    DROP TABLE IF EXISTS `Answer`;\
    DROP TABLE IF EXISTS `Question`;\
    DROP TABLE IF EXISTS `Trainer`;\
    DROP TABLE IF EXISTS `User`;\
"""
Пример #10
0
def connect_db():
    g.db = Connection(config.SPOTIFONT_DB_HOST, config.SPOTIFONT_DB_NAME,
                      config.SPOTIFONT_DB_USER, config.SPOTIFONT_DB_PASSWD)
Пример #11
0
    def __init__(self):

        # refer to db with self.application.db, maintains one db connection

        # cPanel mysql host
        self.db = Connection(host="engr-cpanel-mysql.engr.illinois.edu",
                             user="******",
                             password="******",
                             database="cubucket_db")

        sql = "SELECT name FROM Activity"
        self.trie = Trie()
        results = self.db.query(sql)
        self.activityNames = {}

        trie_words = []
        for result in results:
            trie_words.append(result["name"])
        self.trie.add_token_words(*trie_words)

        # local mysql host
        #self.db = Connection(host='localhost:3306', user='******', password='', database='cucket')  # will later need to change this for heroku

        handlers = [
            tornado.web.URLSpec(r'/', LoginHandler),
            tornado.web.URLSpec(r'/login', LoginHandler),
            tornado.web.URLSpec(r'/logout', LogoutHandler),
            tornado.web.URLSpec(r'/signup', SignupHandler),
            tornado.web.URLSpec(r'/about', AboutHandler),
            tornado.web.URLSpec(r'/activity/new', ActivityHandler),
            tornado.web.URLSpec(r'/user/([a-zA-Z0-9-_]*)', UserHandler),
            tornado.web.URLSpec(r'/home', HomeHandler),
            tornado.web.URLSpec(r'/activity/add/([0-9]+)', RatingHandler),
            tornado.web.URLSpec(r'/activity/delete/([0-9]+)',
                                DeleteActivityHandler),
            tornado.web.URLSpec(r'/search', SearchHandler),
            tornado.web.URLSpec(r'/activity/remove/([0-9]+)',
                                DeleteBucketActivityHandler),
            tornado.web.URLSpec(r'/top', TopHandler),
            tornado.web.URLSpec(r'/search/results', SearchResultsHandler),
            tornado.web.URLSpec(r'/activity/complete/([0-9]+)',
                                CompleteActivityHandler),
            tornado.web.URLSpec(r'/mobile/login', MobileLoginHandler),
            tornado.web.URLSpec(r'/mobile/bucket', MobileUserBucketHandler),
            tornado.web.URLSpec(r'/mobile/complete',
                                MobileCompleteActivityHandler),
            tornado.web.URLSpec(r'/mobile/add', MobileAddActivityHandler),
            tornado.web.URLSpec(r'/category/([a-zA-Z0-9-_]*)', CategoryHandler)
        ]

        current_dir = os.path.dirname(__file__)

        settings = dict(
            template_path=os.path.join(current_dir, 'templates'),
            static_path=os.path.join(current_dir, 'static'),
            debug=options.debug,
            autoescape='xhtml_escape',
            cookie_secret=
            'Dxj43jWAKSag/JbQTmIbBWvpSlBkazj6YGo0A0mo5tyZkb4sTUvT3UH4GU9SXgFuy=',
            xsrf_cookies='True')

        super(Application, self).__init__(handlers, **settings)

        logging.info('Server started on port {0}'.format(options.port))