Mercurial > traipse_dev
comparison plugins/xxcherrypy.py @ 0:4385a7d0efd1 grumpy-goblin
Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author | sirebral |
---|---|
date | Tue, 14 Jul 2009 16:41:58 -0500 |
parents | |
children | 73d9286c22cf |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4385a7d0efd1 |
---|---|
1 import os | |
2 import orpg.plugindb # VEG | |
3 import orpg.pluginhandler | |
4 import thread | |
5 import cherrypy._cpserver as server | |
6 import socket | |
7 import wx | |
8 | |
9 # VEG (march 21, 2007): Now remembers your last web server on/off setting | |
10 | |
11 class Plugin(orpg.pluginhandler.PluginHandler): | |
12 # Initialization subroutine. | |
13 # | |
14 # !self : instance of self | |
15 # !openrpg : instance of the the base openrpg control | |
16 def __init__(self, plugindb, parent): | |
17 orpg.pluginhandler.PluginHandler.__init__(self, plugindb, parent) | |
18 | |
19 # The Following code should be edited to contain the proper information | |
20 self.name = 'CherryPy Web Server' | |
21 self.author = 'Dj Gilcrease' | |
22 self.help = 'This plugin turns OpenRPG into a Web server\n' | |
23 self.help += 'allowing you to host your map and mini files localy' | |
24 | |
25 #You can set variables below here. Always set them to a blank value in this section. Use plugin_enabled | |
26 #to set their proper values. | |
27 self.isServerRunning = 'off' | |
28 self.host = 0 | |
29 | |
30 def plugin_menu(self): | |
31 self.menu = wx.Menu() | |
32 self.toggle = self.menu.AppendCheckItem(wx.ID_ANY, 'On') | |
33 self.topframe.Bind(wx.EVT_MENU, self.cherrypy_toggle, self.toggle) | |
34 | |
35 ports = wx.Menu() | |
36 self.portClient = ports.AppendRadioItem(wx.ID_ANY, 'Client:9557') | |
37 self.portServer = ports.AppendRadioItem(wx.ID_ANY, 'Server:9558') | |
38 self.topframe.Bind(wx.EVT_MENU, self.port_change, self.portClient) | |
39 self.topframe.Bind(wx.EVT_MENU, self.port_change, self.portServer) | |
40 self.menu.AppendMenu(wx.ID_ANY, 'Port', ports) | |
41 | |
42 def port_change(self, evt): | |
43 if self.portClient.IsChecked() == True: self.on_cherrypy("port 9557") | |
44 if self.portServer.IsChecked() == True: self.on_cherrypy("port 9558") | |
45 | |
46 def cherrypy_toggle(self, evt): | |
47 if self.toggle.IsChecked() == True: self.on_cherrypy("on") | |
48 if self.toggle.IsChecked() == False: self.on_cherrypy("off") | |
49 | |
50 def plugin_enabled(self): | |
51 self.port = 9557 | |
52 self.plugin_addcommand('/cherrypy', self.on_cherrypy, '[on | off | port | status] - This controls the CherryPy Web Server') | |
53 tmp = socket.gethostbyname_ex(socket.gethostname()) | |
54 for ip in tmp[2]: | |
55 self.host = ip | |
56 if ip[:7] == '192.168' or ip[:3] == '10.' or ip == '127.0.0.1' or (ip[:3] == '172' and (int(ip[5:6]) >= 16 and int(ip[5:6]) <=32)) : | |
57 self.chat.InfoPost("[WARNING] Cherrypy has detected that you may be behind a router. This is your internal IP. For other users to properly connect, you may have to use your external IP, with port forwarding on port 80.<br />This feature is not suported in any way.") | |
58 | |
59 #if str(self.plugindb.GetString("xxcherrypy", "auto_start", "off")) == "on": # VEG | |
60 # self.on_cherrypy("on") # VEG | |
61 | |
62 def plugin_disabled(self): | |
63 #Here you need to remove any commands you added, and anything else you want to happen when you disable the plugin | |
64 #such as closing windows created by the plugin | |
65 self.plugin_removecmd('/cherrypy') | |
66 if self.isServerRunning == 'on': | |
67 server.stop() | |
68 self.isServerRunning = 'off' | |
69 else: | |
70 pass | |
71 | |
72 | |
73 def on_cherrypy(self, cmdargs): | |
74 args = cmdargs.split(None,-1) | |
75 | |
76 if len(args) == 0 or args[0] == 'status': | |
77 self.chat.InfoPost("CherryPy Web Server is currently: " + self.isServerRunning) | |
78 self.chat.InfoPost("CherryPy Web Server address is: http://" + str(self.host) + ':' + str(self.port) + '/webfiles/') | |
79 | |
80 elif args[0] == 'on' and self.isServerRunning == 'off': | |
81 self.webserver = thread.start_new_thread(self.startServer, (self.port,)) | |
82 self.isServerRunning = 'on' | |
83 self.toggle.Check(True) | |
84 self.plugindb.SetString("xxcherrypy", "auto_start", "on") # VEG | |
85 | |
86 elif args[0] == 'off' and self.isServerRunning == 'on': | |
87 server.stop() | |
88 self.isServerRunning = 'off' | |
89 self.toggle.Check(False) | |
90 self.chat.InfoPost("CherryPy Web Server is now disabled") | |
91 self.plugindb.SetString("xxcherrypy", "auto_start", "off") # VEG | |
92 | |
93 elif args[0] == 'port': | |
94 if self.isServerRunning == 'on': | |
95 self.port = int(args[1]) | |
96 server.stop() | |
97 self.webserver = thread.start_new_thread(self.startServer, (self.port,)) | |
98 self.port = int(args[1]) | |
99 self.chat.InfoPost("CherryPy Web Server is currently: " + self.isServerRunning) | |
100 self.chat.InfoPost("CherryPy Web Server address is: http://" + str(self.host) + ':' + str(self.port) + '/webfiles/') | |
101 | |
102 | |
103 | |
104 def startServer(self, port): | |
105 try: | |
106 if self.host == 0: | |
107 raise Exception("Invalid IP address.<br />This error means you are behind a router or some other form of network that is giving you a Privet IP only (ie. 192.168.x.x, 10.x.x.x, 172.16 - 32.x.x)") | |
108 | |
109 self.chat.InfoPost("CherryPy Web Server is now running on http://" + str(self.host) + ':' + str(self.port) + '/webfiles/') | |
110 server.start(configMap = | |
111 {'staticContentList': [['', r''+orpg.dirpath.dir_struct["user"]+'webfiles/'], | |
112 ['webfiles', r''+orpg.dirpath.dir_struct["user"]+'webfiles/'], | |
113 ['Textures', r''+orpg.dirpath.dir_struct["user"]+'Textures/'], | |
114 ['Maps', r''+orpg.dirpath.dir_struct["user"]+'Maps/'], | |
115 ['Miniatures', r''+orpg.dirpath.dir_struct["user"]+'Miniatures']], | |
116 'socketPort': port, | |
117 'logToScreen': 0, | |
118 'logFile':orpg.dirpath.dir_struct["user"]+'webfiles/log.txt', | |
119 'sessionStorageType':'ram', | |
120 'threadPool':10, | |
121 'sessionTimeout':30, | |
122 'sessionCleanUpDelay':30}) | |
123 | |
124 except Exception, e: | |
125 self.chat.InfoPost("FAILED to start server!") | |
126 self.chat.InfoPost(str(e)) | |
127 self.isServerRunning = 'off' | |
128 self.toggle.Check(False) |