[Pymoul-svn] SF.net SVN: pymoul: [40] pymoul/trunk/src/moul
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-01-17 11:29:32
|
Revision: 40 http://pymoul.svn.sourceforge.net/pymoul/?rev=40&view=rev Author: tiran Date: 2007-01-17 03:29:30 -0800 (Wed, 17 Jan 2007) Log Message: ----------- Added moul.server package. The package contains a list of game servers and two classes to DNS lookup and socket.connect() servers. Added Paths: ----------- pymoul/trunk/src/moul/server/ pymoul/trunk/src/moul/server/__init__.py pymoul/trunk/src/moul/server/ping.py pymoul/trunk/src/moul/server/serverlist.py pymoul/trunk/src/moul/server/tests/ pymoul/trunk/src/moul/server/tests/__init__.py pymoul/trunk/src/moul/server/tests/test_ping.py pymoul/trunk/src/moul/server/tests/test_serverlist.py Added: pymoul/trunk/src/moul/server/__init__.py =================================================================== --- pymoul/trunk/src/moul/server/__init__.py (rev 0) +++ pymoul/trunk/src/moul/server/__init__.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,7 @@ +# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages +try: + __import__('pkg_resources').declare_namespace(__name__) +except ImportError: + from pkgutil import extend_path + __path__ = extend_path(__path__, __name__) + Property changes on: pymoul/trunk/src/moul/server/__init__.py ___________________________________________________________________ Name: svn:eol-style + native Added: pymoul/trunk/src/moul/server/ping.py =================================================================== --- pymoul/trunk/src/moul/server/ping.py (rev 0) +++ pymoul/trunk/src/moul/server/ping.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,135 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Server ping + +>>> server = Server(SERVER_LIST[0], PORT) +>>> result = server.dns() +>>> isinstance(result, float) +True +>>> result = server.portping() +>>> isinstance(result, float) +True + +>>> server = Server('bogus.nonworking.example.foo', PORT) +>>> result = server.dns() +>>> isinstance(result, socket.gaierror) +True + +>>> server = Server(name=SERVER_LIST[0], port=12345, timeout=1.0) +>>> result = server.portping() +>>> isinstance(result, socket.timeout) +True + +>>> serverlist = ServerList(names=SERVER_LIST, port=PORT, timeout=1.0) +>>> for name, stat in serverlist.dns(): +... print name, stat +>>> for name, stat in serverlist.portping(): +... print name, stat + +""" +import socket +from time import time + +from moul.server.serverlist import PORT +from moul.server.serverlist import SERVER_LIST + +def isError(stat): + return isinstance(stat, socket.error) + +class Server(object): + """A server object + """ + + def __init__(self, name, port, timeout=3.0): + self._name = name + self._port = int(port) + self._timeout = float(timeout) + # None: not yet resolved, False: failure, str: IP address + self._ip = None + self._stats = { + 'dns' : None, + 'portping' : None, + } + + def dns(self): + """Resolve IP address + """ + start = time() + try: + ip = socket.gethostbyname(self._name) + except socket.error, msg: + self._ip = False + self._stats['dns'] = msg + return msg + + period = time() - start + self._ip = ip + self._stats['dns'] = period + return period + + def portping(self): + """Connects to the game server port and terminates immediatly + """ + if self._ip is None: + self.dns() + if self._ip is False: + return + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(self._timeout) + start = time() + try: + try: + sock.connect((self._ip, self._port)) + sock.send('\n') + data = sock.recv(1024) + except socket.error, msg: + self._stats['portping'] = msg + return msg + finally: + sock.close() + + period = time() - start + self._stats['portping'] = period + return period + + def __str__(self): + return self._name + + @property + def name(self): + return self._name + +class ServerList(object): + """A list of servers to test + """ + def __init__(self, names, port, timeout=3.0): + self._names = names + self._port = int(port) + self._timeout = float(timeout) + self._servers = [] + for name in names: + self._servers.append(Server(name, port=port, timeout=timeout)) + + def dns(self): + for server in self._servers: + yield server.name, server.dns() + + def portping(self): + for server in self._servers: + yield server.name, server.portping() Property changes on: pymoul/trunk/src/moul/server/ping.py ___________________________________________________________________ Name: svn:eol-style + native Added: pymoul/trunk/src/moul/server/serverlist.py =================================================================== --- pymoul/trunk/src/moul/server/serverlist.py (rev 0) +++ pymoul/trunk/src/moul/server/serverlist.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,53 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Server list +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +PORT = 14617 + +SERVER_LIST = [ + 'beta-auth.urulive.com', + 'beta-file.urulive.com', + 'uruapp-cw01.ibs.aol.com', + 'uruapp-cw02.ibs.aol.com', + 'uruapp-cw03.ibs.aol.com', + 'uruapp-cw04.ibs.aol.com', + 'uruapp-cw05.ibs.aol.com', + 'uruapp-cw06.ibs.aol.com', + 'uruapp-cw07.ibs.aol.com', + 'uruapp-cw08.ibs.aol.com', + 'uruapp-cw09.ibs.aol.com', + 'uruapp-cw10.ibs.aol.com', + 'uruapp-cw11.ibs.aol.com', + 'uruapp-cw12.ibs.aol.com', + 'uruapp-cw13.ibs.aol.com', + 'uruapp-cw14.ibs.aol.com', + 'uruapp-cw15.ibs.aol.com', + 'uruapp-cw16.ibs.aol.com', + 'uruapp-cw17.ibs.aol.com', + ## The servers below are available via ICMP ping but have no running game + ## server (2006-01-17) + #'uruapp-cw18.ibs.aol.com', + #'uruapp-cw19.ibs.aol.com', + #'uruapp-cw20.ibs.aol.com', + #'uruapp-cw21.ibs.aol.com', + #'uruapp-cw22.ibs.aol.com', +] Property changes on: pymoul/trunk/src/moul/server/serverlist.py ___________________________________________________________________ Name: svn:eol-style + native Added: pymoul/trunk/src/moul/server/tests/__init__.py =================================================================== --- pymoul/trunk/src/moul/server/tests/__init__.py (rev 0) +++ pymoul/trunk/src/moul/server/tests/__init__.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,2 @@ +# testing package + Property changes on: pymoul/trunk/src/moul/server/tests/__init__.py ___________________________________________________________________ Name: svn:eol-style + native Added: pymoul/trunk/src/moul/server/tests/test_ping.py =================================================================== --- pymoul/trunk/src/moul/server/tests/test_ping.py (rev 0) +++ pymoul/trunk/src/moul/server/tests/test_ping.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,35 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""moul.server.serverlist unit tests +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +import unittest +from doctest import DocTestSuite + +import moul.server.ping + +def test_suite(): + return unittest.TestSuite(( + DocTestSuite('moul.server.ping'), + )) + +if __name__ == '__main__': + unittest.main(defaultTest="test_suite") Property changes on: pymoul/trunk/src/moul/server/tests/test_ping.py ___________________________________________________________________ Name: svn:eol-style + native Added: pymoul/trunk/src/moul/server/tests/test_serverlist.py =================================================================== --- pymoul/trunk/src/moul/server/tests/test_serverlist.py (rev 0) +++ pymoul/trunk/src/moul/server/tests/test_serverlist.py 2007-01-17 11:29:30 UTC (rev 40) @@ -0,0 +1,35 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""moul.server.serverlist unit tests +""" +__author__ = "Christian Heimes" +__version__ = "$Id$" +__revision__ = "$Revision$" + +import unittest +from doctest import DocTestSuite + +import moul.server.serverlist + +def test_suite(): + return unittest.TestSuite(( + DocTestSuite('moul.server.serverlist'), + )) + +if __name__ == '__main__': + unittest.main(defaultTest="test_suite") Property changes on: pymoul/trunk/src/moul/server/tests/test_serverlist.py ___________________________________________________________________ Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |