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

2014-03-16 22:24:32 +08:00
 tobyzhong
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()
3571 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/104588

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX