我正在 python 烧瓶中为 Jasmin SMS Gateway 编写 Web 服务,以从网关创建和删除用户。如果是 POST 请求,我将调用 runScenario() ,然后我将启动 reactor.run() ,它将在网关中创建用户。此代码在第一次 Web 服务调用时运行完美,但在第二次调用时它给了我这个错误:
raise error.ReactorNotRestartable()
ReactorNotRestartable
这是我的烧瓶应用程序:
#!/usr/bin/env python
from flask import Flask, jsonify, request, Response, make_response, abort
from JasminIntegration import *
JasminWebservice = Flask(__name__)
@JasminWebservice.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@JasminWebservice.route('/jsms/webservice', methods=['POST'])
def create_user():
if not request.json or not 'username' in request.json:
abort(400)
runScenario(request)
reactor.run()
return jsonify({'response':'Success'}), 201
if __name__ == '__main__':
JasminWebservice.run(host="0.0.0.0",port=7034,debug=True)
我正在调用在 JasminIntegration.py 中定义的 runScenario()
#!/usr/bin/env python
import sys
import pickle
from flask import abort
from twisted.internet import defer, reactor
from jasmin.managers.proxies import SMPPClientManagerPBProxy
from jasmin.routing.proxies import RouterPBProxy
from jasmin.routing.Routes import DefaultRoute
from jasmin.routing.jasminApi import SmppClientConnector, User, Group, MtMessagingCredential, SmppsCredential
from jasmin.protocols.smpp.configs import SMPPClientConfig
from twisted.web.client import getPage
@defer.inlineCallbacks
def runScenario(Request):
try:
proxy_router = RouterPBProxy()
yield proxy_router.connect('127.0.0.1', 8988, 'radmin', 'rpwd')
if Request.method == 'POST':
smppUser = Request.json['username']
smppPass = Request.json['password']
smppThroughput = Request.json['tp']
smppBindSessions = Request.json['sessions']
if not smppUser:
abort(400)
if len(smppPass) == 0 or len(smppPass) > 8:
abort(400)
if not smppThroughput.isdigit():
abort(400)
if not smppBindSessions.isdigit():
abort(400)
# Provisiong router with users
smpp_cred = SmppsCredential()
yield smpp_cred.setQuota('max_bindings',int(smppBindSessions))
mt_cred = MtMessagingCredential()
yield mt_cred.setQuota('smpps_throughput' , smppThroughput)
#yield mt_cred.setQuota('submit_sm_count' , 500)
g1 = Group('clients')
u1 = User(uid = smppUser, group = g1, username = smppUser, password = smppPass, mt_credential = mt_cred, smpps_credential = smpp_cred)
yield proxy_router.group_add(g1)
yield proxy_router.user_add(u1)
if Request.method == 'DELETE':
smppUser = Request.json['username']
if not smppUser:
abort(404)
yield proxy_router.user_remove(smppUser)
except Exception, e:
yield "%s" %str(e)
finally:
print "Stopping Reactor"
reactor.stop()
请帮我解决这个问题: