Skip to content

Instantly share code, notes, and snippets.

@joealcorn
Forked from barneygale/mcdowntime.py
Last active December 15, 2015 23:09
Show Gist options
  • Save joealcorn/5337984 to your computer and use it in GitHub Desktop.
Save joealcorn/5337984 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Networking stuff is cribbed from here
https://gist.github.com/barneygale/5051791
This is just a gevent ports
"""
import optparse
import struct
from gevent.server import StreamServer
p = optparse.OptionParser()
p.add_option('-a', '--addr', dest='addr', default='0.0.0.0')
p.add_option('-p', '--port', dest='port', default='25565')
p.add_option('-m', '--message', dest='motd', default='Down for maintenance')
(opt, args) = p.parse_args()
motd = opt.motd
proto_ver = '60'
mc_ver = '1.5.1'
msg = u'\x00'.join((
u'\xa71',
proto_ver,
mc_ver,
motd,
u'99',
u'99'
))
def handler(socket, addr):
print 'Connection from {0}'.format(addr[0])
incoming = socket.recv(1024)
if incoming[0] == '\xfe':
# Pinged from the server list
message = msg
else:
# Somebody is trying to log on
message = motd
s = struct.pack('!h', len(message))
socket.send('\xff' + s + message.encode('utf-16be'))
socket.close()
def serve(ip_addr, port):
print 'Serving on {0}:{1}'.format(ip_addr, port)
server = StreamServer((ip_addr, port), handler)
server.serve_forever()
if __name__ == '__main__':
serve(opt.addr, int(opt.port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment