Junho Kim
Junho Kim

Reputation: 29

Nginx server with uwsgi,flask and sleekxmpp

I'm trying to handling some messages by using nginx server with uwsgi, flask and sleekxmpp. Here is the code.

import ssl, json, logging, threading, time
from flask import Flask
from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout

smsg = """{
        "version":1,
        "type":"request",
        "messageId":"xxyyzz",
        "payload":
        {
                "deviceType":"ctlr",
                "command":"getDeviceInfo"
        }
}"""


class XMPP(ClientXMPP):

    rosterList=[]

    def __init__(self, jid, password):
        ClientXMPP.__init__(self, jid, password)

        self.add_event_handler('session_start', self.session_start, threaded = True)
        self.add_event_handler('message', self.message, threaded=True)
        self.ssl_version = ssl.PROTOCOL_SSLv23

    def session_start(self, event):
        self.send_presence(pshow='online')

        try:
            self.rosterList.append(self.get_roster())
        except IqError as err:
            print 'Error: %' % err.iq['error']['condition']
        except IqTimeout:
            print 'Error: Request time out'

    def message(self, msg):
        data = msg['body'][12:]
        dictData = json.loads(data)
        print data
        if 'payload' in dictData.keys():
            for lists in dictData['payload']['indexes']:
                print lists
        elif 'message' in dictData.keys():
            print 'Request accepted'

app = Flask(__name__)

#logging.basicConfig(level = logging.DEBUG)
xmpp = XMPP('jid', 'password')

class XmppThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        if xmpp.connect(('server', '5222')):
            xmpp.process(block=True)

xt = XmppThread()
xt.start()

@app.route('/')
def send():
    xmpp.send_message(mto='receiver', mbody=smsg, mtype='chat')
    return '<h1>Send</h1>'

I run the code by uwsgi with these options.

[uwsgi]
uid = uwsgi
gid = uwsgi
pidfile = /run/uwsgi/uwsgi.pid
emperor = /etc/uwsgi.d
stats = /run/uwsgi/stats.sock
chmod-socket = 660
emperor-tyrant = true
cap = setgid,setuid


[uwsgi]
plugin = python
http-socket = :8080
wsgi-file = /var/www/uwsgi/flask_uwsgi.py
callable = app
module = app
enable-threads = True
logto = /var/www/uwsgi/flask_uwsgi.log

When I run uwsgi by typing command, like '/usr/sbin/uwsgi --ini uwsgi.ini', it works well. I can send and recieve the messages. But, when I run this on CentOS 7's service, recieve is working, but send is not working. Did i need some more options or missing something?

Upvotes: 1

Views: 175

Answers (0)

Related Questions