V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tobyzhong
V2EX  ›  Python

关于脚本有相对目录问题然后设置启动脚本去调用这个程序的问题

  •  
  •   tobyzhong · 2014-03-16 22:24:32 +08:00 · 3569 次点击
    这是一个创建于 3903 天前的主题,其中的信息可能已经有所发展或是发生改变。
    https://github.com/isombyt/XiaQian
    我在github上看到这个虾米自动签到的脚本,但是运行这个脚本之后发现,你必须进入XiaQian,这个目录,再运行webmanager.py才能够成功执行,不然就会报错,也就是相对目录的问题。
    并且,我是ubuntu系统,我的想法是 写一个python脚本去调用这个签到程序,然后,把它放在开机启动项。
    或者有人能告诉我怎么系统里直接设置成开机子启动。这里有一个比较大的问题是,这个脚本必须进入之前那个目录才能运行。
    import web
    import gevent
    import gevent.monkey
    import gevent.pywsgi
    from db import xiamidb
    import time
    import traceback
    import os
    from libXiami import User
    gevent.monkey.patch_all()


    class Main:

    def GET(self):
    return file("static/index.html").read()

    def POST(self):
    args = web.input()
    email = args.pop("email")
    password = args.pop("password")
    user = User()
    user.login(email,password)
    if user.islogined:
    xiamidb.add(user["user_id"],email,password)
    account = xiamidb.get(user["user_id"])
    account["last"] = time.time()
    account["cookie"] = user.dumpCookie()
    account["email"] = email
    if "savepw" in args:
    account["password"] = password
    else:
    account["password"] = None
    if "notifyme" in args:
    account["notify"] = True
    else:
    account["notify"] = False
    account["errcount"] = 0
    account["nexttime"] = account["last"]
    account["days"] = int(user["sign"]["persist_num"])
    xiamidb.update(**account)
    yield "email:%s\n"%email
    yield "uid:%s\n"%account['uid']
    yield "cookie:\n%s"%account['cookie']
    yield "days:%s\n"%account["days"]
    yield '\xe7\x99\xbb\xe8\xae\xb0\xe6\x88\x90\xe5\x8a\x9f\xef\xbc\x8c\xe8\xaf\xb7\xe5\x85\xb3\xe9\x97\xad\xe9\xa1\xb5\xe9\x9d\xa2'
    else:
    yield '\xe6\x97\xa0\xe6\xb3\x95\xe8\x8e\xb7\xe5\x8f\x96\xe7\x94\xa8\xe6\x88\xb7\xe4\xbf\xa1\xe6\x81\xaf\xef\xbc\x8c\xe8\xaf\xb7\xe6\xa3\x80\xe6\x9f\xa5\xe5\xb8\x90\xe5\x8f\xb7\xe6\x88\x96\xe5\xaf\x86\xe7\xa0\x81'


    class Ctrl:

    def GET(self):
    yield "uid\temail\t\t\tdays\tlastcheck\tnextcheck\terrcount\tstatus\n"
    accounts = xiamidb.scan()
    for account in accounts:
    account["last"] -= time.time()
    account["nexttime"] -= time.time()
    yield "%(uid)s\t%(email)s\t%(days)s\t%(last)s\t%(nexttime)s\t%(errcount)s\t%(status)s\n"%account

    class StaticFile:

    def GET(self,filename):
    path = "static/%s"%filename
    if os.path.exists(path):
    return file(path,"rb")
    return "404"


    def checkin(account):
    user = User()
    user.loadCookie(account["cookie"])
    if not user.islogined and account["password"]:
    user.login(account["email"],account["password"])
    if user.islogined:
    if not user.ischeckined:
    days = user.checkin()
    if days:
    account["days"] = days
    else:
    account["errcount"] += 1
    account["nexttime"] = time.time() + 600
    account["status"] = u'\u7b7e\u5230\u5931\u8d25'
    else:
    days = int(user["sign"]["persist_num"])
    account["nexttime"] = time.time() + 3600
    account["status"] = u'\u6ca1\u6709\u5f02\u5e38'
    else:
    account["errcount"] += 1
    account["nexttime"] = time.time() + 600
    account["status"] = u'\u767b\u5f55\u5931\u8d25'
    account["last"] = time.time()
    xiamidb.update (**account)

    def daemon():
    while True:
    accounts = xiamidb.scan(where="errcount<3 and nexttime<%s"%time.time())
    for account in accounts:
    try:
    checkin(account)
    except:
    traceback.print_exc()
    print "checkin error"
    time.sleep(5)
    time.sleep(5)

    urls = (
    "/", "Main",
    "/Ctrl","Ctrl",
    "/(.*)","StaticFile",
    )


    def serve_forever():
    gevent.spawn(daemon)
    print web
    application = web.application(urls, globals()).wsgifunc()
    print 'Serving on 8888'
    server = gevent.pywsgi.WSGIServer(('', 8888), application)
    server.serve_forever()

    if __name__ == "__main__":
    serve_forever()
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   979 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:37 · PVG 04:37 · LAX 12:37 · JFK 15:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.