import sys
from wx import Frame, DefaultPosition, Size, Menu, MenuBar, App
from wx import EVT_MENU, EVT_CLOSE
from twisted.python import log
from twisted.internet import wxreactor
wxreactor.install()
# import t.i.reactor only after installing wxreactor:
from twisted.internet import protocol, reactor, defer
from twisted.protocols import basic
from twisted.enterprise import adbapi
from twisted.web import xmlrpc, server
dbpool = adbapi.ConnectionPool("sqlite3", 'ren.db')
#import row_object
ID_EXIT = 101
def sql_gen(i):
return """select * from %s;""" % i
def select_bd(table):
return dbpool.runQuery(sql_gen(table))
def printResult(i):
if len(i):
print i
return `i`
else:
return '0'
class MyFrame(Frame):
def __init__(self, parent, ID, title):
Frame.__init__(self, parent, ID, title, DefaultPosition, Size(300, 200))
menu = Menu()
menu.Append(ID_EXIT, "E&xit", "Terminate the program")
menuBar = MenuBar()
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)
EVT_MENU(self, ID_EXIT, self.DoExit)
# make sure reactor.stop() is used to stop event loop:
EVT_CLOSE(self, lambda evt: reactor.stop())
def DoExit(self, event):
reactor.stop()
class MyApp(App, xmlrpc.XMLRPC):
allowNone=False
useDateTime=False
def OnInit(self):
frame = MyFrame(None, -1, "Hello, world")
#frame.Show(True)
#self.SetTopWindow(frame)
# look, we can use twisted calls!
return True
def xmlrpc_select(self,table):
print self.server()
return select_bd(table).addCallback(printResult)
def demo():
log.startLogging(sys.stdout)
# register the App instance with Twisted:
app = MyApp(0)
reactor.registerWxApp(app)
reactor.listenTCP(8000, server.Site(app))
# start the event loop:
reactor.run()
if __name__ == '__main__':
demo()
1. Все ли здесь правильно с асинхронностью?
2. Как извлечь имя хоста(IP адрес) клиента?
Документацию уже изучил вдоль и поперек. Слишком она запутана. Хоть бы один маленький пример боле мене приближенный к жизни.