summaryrefslogtreecommitdiff
path: root/metaserver
diff options
context:
space:
mode:
authorDiego Roversi <diegor@tiscali.it>2019-09-08 22:05:57 +0200
committerDiego Roversi <diegor@tiscali.it>2019-09-08 22:05:57 +0200
commitc23b138ed72d794521b19ff7d8076b32b0b7bf46 (patch)
treec8e82a4ca4da05ba1ad28d4ff64a157bc1eb7952 /metaserver
parent1d9925c287b318ec21343e2682b51ab6a36ae8db (diff)
2to3
Diffstat (limited to 'metaserver')
-rw-r--r--metaserver/metaclient.py162
-rw-r--r--metaserver/metaserver.py50
-rw-r--r--metaserver/metastruct.py4
-rw-r--r--metaserver/pipelayer.py16
-rw-r--r--metaserver/socketoverudp.py4
5 files changed, 118 insertions, 118 deletions
diff --git a/metaserver/metaclient.py b/metaserver/metaclient.py
index aedb99c..828d2dd 100644
--- a/metaserver/metaclient.py
+++ b/metaserver/metaclient.py
@@ -1,7 +1,7 @@
import sys, os, time, random
from select import select
from socket import *
-from metastruct import *
+from .metastruct import *
_SERVER = 'ctpug.org.za'
@@ -13,16 +13,16 @@ VERSION_TAG = 1601
def connect(failure=[]):
if len(failure) >= 2:
return None
- print >> sys.stderr, 'Connecting to the meta-server %s:%d...' % METASERVER
+ print('Connecting to the meta-server %s:%d...' % METASERVER, file=sys.stderr)
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect(METASERVER)
- except error, e:
- print >> sys.stderr, '*** cannot contact meta-server:', str(e)
+ except error as e:
+ print('*** cannot contact meta-server:', str(e), file=sys.stderr)
failure.append(e)
return None
else:
- print >> sys.stderr, 'connected.'
+ print('connected.', file=sys.stderr)
return s
sys.setcheckinterval(4096)
@@ -30,7 +30,7 @@ sys.setcheckinterval(4096)
def float2str(f):
# don't trust locale issues and write a string with a '.'
- s = str(long(f*1000000.0))
+ s = str(int(f*1000000.0))
return s[:-6] + '.' + s[-6:]
def str2float(s):
@@ -76,19 +76,19 @@ class MetaClientSrv(MessageSocket):
import gamesrv
gamesrv.removesocket('META', self.s)
self.closed = 1
- print >> sys.stderr, 'disconnected from the meta-server'
+ print('disconnected from the meta-server', file=sys.stderr)
def send_traceback(self):
if not self.closed:
- import traceback, cStringIO, sys
- f = cStringIO.StringIO()
- print >> f, sys.version
- print >> f, 'platform: ', sys.platform
- print >> f, 'executable: ', sys.executable
- print >> f, 'argv: ', sys.argv
- print >> f, 'cwd: ', os.getcwd()
- print >> f, 'version tag:', VERSION_TAG
- print >> f
+ import traceback, io, sys
+ f = io.StringIO()
+ print(sys.version, file=f)
+ print('platform: ', sys.platform, file=f)
+ print('executable: ', sys.executable, file=f)
+ print('argv: ', sys.argv, file=f)
+ print('cwd: ', os.getcwd(), file=f)
+ print('version tag:', VERSION_TAG, file=f)
+ print(file=f)
traceback.print_exc(file = f)
self.s.sendall(message(MMSG_TRACEBACK, f.getvalue()))
@@ -100,37 +100,37 @@ class MetaClientSrv(MessageSocket):
if self.lastwakeup == wakeup:
sys.setcheckinterval(4096)
self.synsockets.clear()
- import thread
+ import _thread
self.lastwakeup = time.time()
- thread.start_new_thread(fastresponses, (self.lastwakeup,))
+ _thread.start_new_thread(fastresponses, (self.lastwakeup,))
def msg_connect(self, origin, port, *rest):
def connect(origin, port):
host, _ = origin.split(':')
addr = host, port
s = socket(AF_INET, SOCK_STREAM)
- print >> sys.stderr, 'backconnecting to', addr
+ print('backconnecting to', addr, file=sys.stderr)
try:
s.connect(addr)
- except error, e:
- print >> sys.stderr, 'backconnecting:', str(e)
+ except error as e:
+ print('backconnecting:', str(e), file=sys.stderr)
else:
self.game.newclient_threadsafe(s, addr)
- import thread
- thread.start_new_thread(connect, (origin, port))
+ import _thread
+ _thread.start_new_thread(connect, (origin, port))
def msg_udp_conn(self, origin, secret, port, *rest):
def connect(origin, secret, port):
host, _ = origin.split(':')
addr = host, port
s = socket(AF_INET, SOCK_DGRAM)
- print >> sys.stderr, 'udp connecting to', addr
+ print('udp connecting to', addr, file=sys.stderr)
s.connect(addr)
mysecret = random.randrange(0, 65536)
packet = ('B' + chr( secret & 0xFF) + chr( secret >> 8)
+ chr(mysecret & 0xFF) + chr(mysecret >> 8))
- from socketoverudp import SocketOverUdp
- from socketoverudp import SOU_RANGE_START, SOU_RANGE_STOP
+ from .socketoverudp import SocketOverUdp
+ from .socketoverudp import SOU_RANGE_START, SOU_RANGE_STOP
for i in range(5):
#print 'sending', repr(packet)
s.send(packet)
@@ -153,7 +153,7 @@ class MetaClientSrv(MessageSocket):
SOU_RANGE_START <= ord(inbuf[0]) < SOU_RANGE_STOP):
break
else:
- print >> sys.stderr, 'udp connecting: no answer, giving up'
+ print('udp connecting: no answer, giving up', file=sys.stderr)
return
sock = SocketOverUdp(s, (mysecret, secret))
data = sock._decode(inbuf)
@@ -163,13 +163,13 @@ class MetaClientSrv(MessageSocket):
#print 'waiting for more'
iwtd, owtd, ewtd = select([sock], [], [], 5.0)
if sock not in iwtd:
- print >> sys.stderr, 'udp connecting: timed out'
+ print('udp connecting: timed out', file=sys.stderr)
return
#print 'decoding more'
data += sock.recv()
#print 'now data is', repr(data)
if data[:-2] != expected:
- print >> sys.stderr, 'udp connecting: bad data'
+ print('udp connecting: bad data', file=sys.stderr)
return
sock.sendall('[bnb s->c]' + data[-2:])
sock.flush()
@@ -177,19 +177,19 @@ class MetaClientSrv(MessageSocket):
while 1:
iwtd, owtd, ewtd = select([sock], [], [], 5.0)
if sock not in iwtd:
- print >> sys.stderr, 'udp connecting: timed out'
+ print('udp connecting: timed out', file=sys.stderr)
return
data = sock.recv(200)
if data:
break
if data != '^':
- print >> sys.stderr, 'udp connecting: bad data'
+ print('udp connecting: bad data', file=sys.stderr)
return
#print 'done!'
self.game.newclient_threadsafe(sock, addr)
- import thread
- thread.start_new_thread(connect, (origin, secret, port))
+ import _thread
+ _thread.start_new_thread(connect, (origin, secret, port))
def msg_ping(self, origin, *rest):
# ping time1 --> pong time2 time1
@@ -213,15 +213,15 @@ class MetaClientSrv(MessageSocket):
#print 'sleep(%r)' % delay
if 0.0 <= delay <= 10.0:
time.sleep(delay)
- print >> sys.stderr, 'synconnecting to', addr
+ print('synconnecting to', addr, file=sys.stderr)
try:
s.connect(addr)
- except error, e:
- print >> sys.stderr, 'synconnecting:', str(e)
+ except error as e:
+ print('synconnecting:', str(e), file=sys.stderr)
else:
self.game.newclient_threadsafe(s, addr)
- import thread
- thread.start_new_thread(connect, (origin, clientport, connecttime, s))
+ import _thread
+ _thread.start_new_thread(connect, (origin, clientport, connecttime, s))
MESSAGES = {
RMSG_CONNECT: msg_connect,
@@ -266,8 +266,8 @@ def meta_unregister(game):
class Event:
def __init__(self):
- import thread
- self.lock = thread.allocate_lock()
+ import _thread
+ self.lock = _thread.allocate_lock()
self.lock.acquire()
def signal(self):
try:
@@ -288,24 +288,24 @@ class MetaClientCli:
self.threads = {}
def run(self):
- import thread
- print >> sys.stderr, 'Trying to connect to', self.serverkey
+ import _thread
+ print('Trying to connect to', self.serverkey, file=sys.stderr)
self.ev = Event()
self.ev2 = Event()
self.buffer = ""
- self.sendlock = thread.allocate_lock()
- self.recvlock = thread.allocate_lock()
+ self.sendlock = _thread.allocate_lock()
+ self.recvlock = _thread.allocate_lock()
self.inputmsgqueue = []
self.gotudpport = None
if not (PORTS.get('CLIENT') or PORTS.get('sendudpto')):
self.s = connect()
- thread.start_new_thread(self.acquire_udp_port, ())
+ _thread.start_new_thread(self.acquire_udp_port, ())
else:
self.s = None
self.ev2.signal()
self.startthread(self.try_udp_connect)
- thread.start_new_thread(self.bipbip, ())
+ _thread.start_new_thread(self.bipbip, ())
self.startthread(self.try_direct_connect, 0.75)
self.startthread(self.try_indirect_connect, 1.50)
while self.resultsocket is None:
@@ -323,7 +323,7 @@ class MetaClientCli:
self.ev.signal()
def startthread(self, fn, sleep=0.0, args=()):
- import thread
+ import _thread
def bootstrap(fn, atom, sleep, args):
try:
time.sleep(sleep)
@@ -334,21 +334,21 @@ class MetaClientCli:
self.ev.signal()
atom = object()
self.threads[atom] = time.time()
- thread.start_new_thread(bootstrap, (fn, atom, sleep, args))
+ _thread.start_new_thread(bootstrap, (fn, atom, sleep, args))
def threadsleft(self):
if self.fatalerror:
sys.exit(1)
now = time.time()
TIMEOUT = 11
- for starttime in self.threads.values():
+ for starttime in list(self.threads.values()):
if now < starttime + TIMEOUT:
break
else:
if self.threads:
- print >> sys.stderr, '*** time out, giving up.'
+ print('*** time out, giving up.', file=sys.stderr)
else:
- print >> sys.stderr, '*** failed to connect.'
+ print('*** failed to connect.', file=sys.stderr)
sys.exit(1)
def try_direct_connect(self):
@@ -357,14 +357,14 @@ class MetaClientCli:
s = socket(AF_INET, SOCK_STREAM)
try:
s.connect((host, port))
- except error, e:
- print >> sys.stderr, 'direct connexion failed:', str(e)
+ except error as e:
+ print('direct connexion failed:', str(e), file=sys.stderr)
else:
- print >> sys.stderr, 'direct connexion accepted.'
+ print('direct connexion accepted.', file=sys.stderr)
self.resultsocket = s
def try_indirect_connect(self):
- import thread, time
+ import _thread, time
if not self.s:
self.s = connect()
if not self.s:
@@ -413,8 +413,8 @@ class MetaClientCli:
self.gotudpport = msg[1], int(msg[2])
continue
if msg[0] == RMSG_NO_HOST and msg[1] == self.serverkey:
- print >> sys.stderr, ('*** server %r is not registered'
- ' on the meta-server' % (msg[1],))
+ print(('*** server %r is not registered'
+ ' on the meta-server' % (msg[1],)), file=sys.stderr)
self.fatalerror = True
sys.exit()
self.inputmsgqueue.append(msg)
@@ -425,7 +425,7 @@ class MetaClientCli:
return
data = self.s.recv(2048)
if not data:
- print >> sys.stderr, 'disconnected from the meta-server'
+ print('disconnected from the meta-server', file=sys.stderr)
sys.exit()
self.buffer += data
finally:
@@ -441,11 +441,11 @@ class MetaClientCli:
s1.listen(1)
_, port = s1.getsockname()
self.routemsg(RMSG_CONNECT, port)
- print >> sys.stderr, 'listening for backward connection'
+ print('listening for backward connection', file=sys.stderr)
iwtd, owtd, ewtd = select([s1], [], [], 7.5)
if s1 in iwtd:
s, addr = s1.accept()
- print >> sys.stderr, 'accepted backward connection from', addr
+ print('accepted backward connection from', addr, file=sys.stderr)
self.resultsocket = s
def send_ping(self):
@@ -459,11 +459,11 @@ class MetaClientCli:
remoteaddr = remotehost, remoteport
try:
s.connect(remoteaddr)
- except error, e:
- print >> sys.stderr, 'SYN connect failed:', str(e)
+ except error as e:
+ print('SYN connect failed:', str(e), file=sys.stderr)
return
- print >> sys.stderr, ('simultaneous SYN connect succeeded with %s:%d' %
- remoteaddr)
+ print(('simultaneous SYN connect succeeded with %s:%d' %
+ remoteaddr), file=sys.stderr)
self.resultsocket = s
def try_udp_connect(self):
@@ -500,7 +500,7 @@ class MetaClientCli:
for name in ('*udpsock*', 'CLIENT'):
if name in PORTS:
del PORTS[name]
- from socketoverudp import SocketOverUdp
+ from .socketoverudp import SocketOverUdp
sock = SocketOverUdp(s, (originalsecret, remotesecret))
#print 'sending', repr(packet)
sock.sendall(packet)
@@ -511,24 +511,24 @@ class MetaClientCli:
#print 'waiting'
iwtd, owtd, ewtd = select([sock], [], [], 2.5)
if sock not in iwtd:
- print >> sys.stderr, 'socket-over-udp timed out'
+ print('socket-over-udp timed out', file=sys.stderr)
return
#print 'we get:'
data += sock.recv()
#print repr(data)
if data != expected:
- print >> sys.stderr, 'bad udp data from', addr
+ print('bad udp data from', addr, file=sys.stderr)
else:
sock.sendall('^')
sock.flush()
- print 'udp connexion handshake succeeded'
+ print('udp connexion handshake succeeded')
self.resultsocket = sock
def acquire_udp_port(self):
try:
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', INADDR_ANY))
- randomdata = hex(random.randrange(0, sys.maxint))
+ randomdata = hex(random.randrange(0, sys.maxsize))
for i in range(5):
s.sendto(randomdata, METASERVER_UDP)
time.sleep(0.05)
@@ -538,9 +538,9 @@ class MetaClientCli:
if self.gotudpport:
PORTS['*udpsock*'] = s, self.gotudpport
udphost, udpport = self.gotudpport
- print >> sys.stderr, ('udp port %d is visible from '
+ print(('udp port %d is visible from '
'outside on %s:%d' % (
- s.getsockname()[1], udphost, udpport))
+ s.getsockname()[1], udphost, udpport)), file=sys.stderr)
self.startthread(self.try_udp_connect)
break
finally:
@@ -568,13 +568,13 @@ def print_server_list():
assert msg[0] == RMSG_LIST
entries = decodedict(msg[1])
if not entries:
- print >> sys.stderr, 'No registered server.'
+ print('No registered server.', file=sys.stderr)
else:
- print
- print ' %-25s | %-30s | %s' % (
- 'server', 'game', 'players')
- print '-'*27+'+'+'-'*32+'+'+'-'*11
- for key, value in entries.items():
+ print()
+ print(' %-25s | %-30s | %s' % (
+ 'server', 'game', 'players'))
+ print('-'*27+'+'+'-'*32+'+'+'-'*11)
+ for key, value in list(entries.items()):
if ':' in key:
try:
addr, _, _ = gethostbyaddr(key[:key.index(':')])
@@ -585,12 +585,12 @@ def print_server_list():
if len(addr) < 28: addr += '|'
addr = '%-60s' % (addr,)
if len(addr) < 61: addr += '|'
- print addr
+ print(addr)
value = decodedict(value)
- print ' %-25s | %-30s | %s' % (
+ print(' %-25s | %-30s | %s' % (
key, value.get('desc', '<no description>'),
- value.get('extradesc', ''))
- print
+ value.get('extradesc', '')))
+ print()
if __name__ == '__main__':
print_server_list()
diff --git a/metaserver/metaserver.py b/metaserver/metaserver.py
index cb4ea19..73c6ee3 100644
--- a/metaserver/metaserver.py
+++ b/metaserver/metaserver.py
@@ -1,9 +1,9 @@
from socket import *
import os, sys, time, random
from select import select
-from cStringIO import StringIO
+from io import StringIO
from weakref import WeakValueDictionary
-from metastruct import *
+from .metastruct import *
from common import httpserver, stdlog
if __name__ == '__main__':
@@ -40,14 +40,14 @@ class MetaServer:
def detach(self):
pid = os.fork()
if pid:
- print pid
+ print(pid)
os._exit(0)
# in the child process
os.setsid()
logfile = stdlog.LogFile(limitsize=131072)
if logfile:
- print >> logfile
- print "Logging to", logfile.filename
+ print(file=logfile)
+ print("Logging to", logfile.filename)
fd = logfile.f.fileno()
try:
# detach from parent
@@ -59,7 +59,7 @@ class MetaServer:
logfile.close()
# record pid
f = open('pid', 'w')
- print >> f, os.getpid()
+ print(os.getpid(), file=f)
f.close()
def clientconnect(self):
@@ -77,7 +77,7 @@ class MetaServer:
raise OverflowError
self.ServersList.append(server)
self.ServersDict[key] = server
- print '+', key
+ print('+', key)
def unpublish(self, server):
key = server.serverkey
@@ -86,7 +86,7 @@ class MetaServer:
if current is server:
del self.ServersDict[key]
self.ServersList.remove(server)
- print '-', key
+ print('-', key)
def makelist(self):
items = {}
@@ -116,7 +116,7 @@ class Connexion(MessageSocket):
self.addr = addr
self.key = '%s:%d' % addr
self.serverkey = None
- print '[', self.key
+ print('[', self.key)
self.backlinks = WeakValueDictionary()
if len(serversockets) >= MAX_CONNEXIONS:
self.disconnect()
@@ -129,10 +129,10 @@ class Connexion(MessageSocket):
del serversockets[self.s]
except KeyError:
pass
- print ']', self.key
+ print(']', self.key)
def msg_serverinfo(self, info, *rest):
- print '|', self.key
+ print('|', self.key)
if len(info) > 15000:
raise OverflowError
info = decodedict(info)
@@ -169,7 +169,7 @@ class Connexion(MessageSocket):
def msg_traceback(self, tb, *rest):
f = stdlog.LogFile('tb-%s.log' % (self.addr[0],))
if f:
- print >> f, tb
+ print(tb, file=f)
f.close()
def msg_udp_addr(self, pattern, *rest):
@@ -198,9 +198,9 @@ class Connexion(MessageSocket):
# ____________________________________________________________
-import htmlentitydefs
+import html.entities
text_to_html = {}
-for key, value in htmlentitydefs.entitydefs.items():
+for key, value in list(html.entities.entitydefs.items()):
text_to_html[value] = '&' + key + ';'
for i in range(32):
text_to_html[chr(i)] = '?'
@@ -305,7 +305,7 @@ httpserver.register('mbub.png', httpserver.fileloader('mbub.png', 'image/png'))
httpserver.register('home.png', httpserver.fileloader('home.png', 'image/png'))
def openhttpsocket(port = META_SERVER_HTTP_PORT):
- from BaseHTTPServer import HTTPServer
+ from http.server import HTTPServer
class ServerClass(HTTPServer):
def get_request(self):
sock, addr = self.socket.accept()
@@ -322,19 +322,19 @@ def openhttpsocket(port = META_SERVER_HTTP_PORT):
def mainloop():
while 1:
- iwtd = serversockets.keys()
+ iwtd = list(serversockets.keys())
iwtd, owtd, ewtd = select(iwtd, [], iwtd)
#print iwtd, owtd, ewtd, serversockets
for s in iwtd:
if s in serversockets:
input, close = serversockets[s]
try:
- input()
+ eval(input())
except:
import traceback
- print "-"*60
+ print("-"*60)
traceback.print_exc()
- print "-"*60
+ print("-"*60)
for s in ewtd:
if s in serversockets:
input, close = serversockets[s]
@@ -342,9 +342,9 @@ def mainloop():
close()
except:
import traceback
- print "-"*60
+ print("-"*60)
traceback.print_exc()
- print "-"*60
+ print("-"*60)
if __name__ == '__main__':
@@ -353,13 +353,13 @@ if __name__ == '__main__':
metaserver.detach()
try:
openhttpsocket()
- print 'listening to client port tcp %d / http %d / udp %d.' % (
+ print('listening to client port tcp %d / http %d / udp %d.' % (
META_SERVER_PORT,
META_SERVER_HTTP_PORT,
- META_SERVER_UDP_PORT)
+ META_SERVER_UDP_PORT))
mainloop()
finally:
if metaserver.ServersList:
- print '*** servers still connected, waiting 5 seconds'
+ print('*** servers still connected, waiting 5 seconds')
time.sleep(5)
- print '*** leaving at', time.ctime()
+ print('*** leaving at', time.ctime())
diff --git a/metaserver/metastruct.py b/metaserver/metastruct.py
index 13240cc..e187b65 100644
--- a/metaserver/metastruct.py
+++ b/metaserver/metastruct.py
@@ -27,7 +27,7 @@ RMSG_NO_HOST = '?'
def encodedict(dict):
data = []
- for key, value in dict.items():
+ for key, value in list(dict.items()):
data.append(message('#', key, value))
return ''.join(data)
@@ -69,7 +69,7 @@ class MessageSocket:
if msg is None:
break
if msg[0] not in self.MESSAGES:
- print >> sys.stderr, 'unknown message %r' % (msg[0],)
+ print('unknown message %r' % (msg[0],), file=sys.stderr)
else:
fn = self.MESSAGES[msg[0]]
fn(self, *msg[1:])
diff --git a/metaserver/pipelayer.py b/metaserver/pipelayer.py
index 9baf78a..82a8cd9 100644
--- a/metaserver/pipelayer.py
+++ b/metaserver/pipelayer.py
@@ -209,7 +209,7 @@ class PipeLayer(object):
def bad_crc(self):
import sys
- print >> sys.stderr, "warning: bad crc on udp connexion"
+ print("warning: bad crc on udp connexion", file=sys.stderr)
self.in_outoforder.clear()
self.out_flags = FLAG_NAK
self.out_nextrepeattime = self.cur_time
@@ -217,15 +217,15 @@ class PipeLayer(object):
_dump_indent = 0
def dump(self, dir, rawdata):
in_flags, ack_seqid, in_seqid = struct.unpack("!BBH", rawdata[:4])
- print ' ' * self._dump_indent, dir,
+ print(' ' * self._dump_indent, dir, end=' ')
if in_flags == FLAG_NAK:
- print 'NAK',
+ print('NAK', end=' ')
elif in_flags == FLAG_NAK1:
- print 'NAK1',
+ print('NAK1', end=' ')
elif in_flags == FLAG_CFRM:
- print 'CFRM',
+ print('CFRM', end=' ')
#print ack_seqid, in_seqid, '(%d bytes)' % (len(rawdata)-4,)
- print ack_seqid, in_seqid, repr(rawdata[4:])
+ print(ack_seqid, in_seqid, repr(rawdata[4:]))
def pipe_over_udp(udpsock, send_fd=-1, recv_fd=-1,
@@ -281,11 +281,11 @@ def pipe_over_udp(udpsock, send_fd=-1, recv_fd=-1,
class PipeOverUdp(object):
def __init__(self, udpsock, timeout=1.0):
- import thread, os
+ import _thread, os
self.os = os
self.sendpipe = os.pipe()
self.recvpipe = os.pipe()
- thread.start_new_thread(pipe_over_udp, (udpsock,
+ _thread.start_new_thread(pipe_over_udp, (udpsock,
self.sendpipe[0],
self.recvpipe[1],
timeout))
diff --git a/metaserver/socketoverudp.py b/metaserver/socketoverudp.py
index 2df4a75..14565c8 100644
--- a/metaserver/socketoverudp.py
+++ b/metaserver/socketoverudp.py
@@ -1,6 +1,6 @@
from time import time as now
-from pipelayer import PipeLayer, InvalidPacket
-from pipelayer import FLAG_RANGE_START, FLAG_RANGE_STOP
+from .pipelayer import PipeLayer, InvalidPacket
+from .pipelayer import FLAG_RANGE_START, FLAG_RANGE_STOP
import socket, struct
SOU_RANGE_START = FLAG_RANGE_START