Python Server Wrapper

Discussion in 'Bukkit Discussion' started by skyler moore, May 30, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    skyler moore

    Hey guys this is my first post on the forums just so you know. i was looking around the forums and couldn't find a server wrapper to handle simple things with the server so I made my own server wrapper in python. The wrapper doesn't do much but you can make it grab the latest version of craftbukkit and the latest version of the default mincraft server. it uses simple command line options to control everything. Edit it as you need to, it was intended just for my use, but i might update it here later if i change it.

    Download:
    [​IMG]

    Help:

    usage: run.py [-h] [-u] [-n] [-f UPDATEURL] [--min MINRAM] [--max MAXRAM]
    [-P PDIR] [-c CFILE] [-H HOST] [-o ONLINE] [-p PORT] [-s SIZE]
    [-w WORLD] [-v]

    This will control the server

    optional arguments:
    -h, --help show this help message and exit
    -u Will get the latest version of the craftbukkit, can be used
    with -n to update the default server
    -n Will run the default server not craftbukkit, if used with -u
    it will update the default server
    -f UPDATEURL Will force an URL update, good for updating to beta versions
    --min MINRAM Will set the min amount of ram, default 1024M
    --max MAXRAM Will set the max amount of ram, default 1024M
    -P PDIR Will allow you to define the plugin directory, default plugins
    -c CFILE Will allow you to define the properties file, default
    server.properties
    -H HOST Host to listen on
    -o ONLINE Weather to use online authentication (1 for online, 0 for
    offline)
    -p PORT States the port to listen on
    -s SIZE defines the max amount of players
    -w WORLD World directory
    -v show program's version number and exit


    Source:
    Code:
    import argparse
    import urllib2
    import os
    import time
    import glob
    
    run=1
    skipbukkit=0
    maxram='1024M'
    minram='1024M'
    plugins=''
    config=''
    host=''
    online=''
    port=''
    players=''
    world=''
    
    parser = argparse.ArgumentParser(description='This will control the server')
    parser.add_argument('-u', dest='update', action='store_const', const=1, default=0,
                       help='Will get the latest version of the craftbukkit, can be used with -n to update the default server')
    parser.add_argument('-n', dest='nobukkit', action='store_const', const=1, default=0,
                       help='Will run the default server not craftbukkit, if used with -u it will update the default server')
    parser.add_argument('-f', dest='updateurl', action='store', default=0,
                       help='Will force an URL update, good for updating to beta versions')
    parser.add_argument('--min', dest='minram', action='store', default=1024,
                       help='Will set the min amount of ram, default 1024M')
    parser.add_argument('--max', dest='maxram', action='store', default=1024,
                       help='Will set the max amount of ram, default 1024M')
    parser.add_argument('-P', dest='Pdir', action='store', default=0,
                       help='Will allow you to define the plugin directory, default plugins')
    parser.add_argument('-c', dest='cfile', action='store', default=0,
                       help='Will allow you to define the properties file, default server.properties')
    parser.add_argument('-H', dest='host', action='store', default=0,
                       help='Host to listen on')
    parser.add_argument('-o', dest='online', action='store', default=1,
                       help='Weather to use online authentication (1 for online, 0 for offline)')
    parser.add_argument('-p', dest='port', action='store', default=0,
                       help='States the port to listen on')
    parser.add_argument('-s', dest='size', action='store', default=0,
                       help='defines the max amount of players')
    parser.add_argument('-w', dest='world', action='store', default=0,
                       help='World directory')
    parser.add_argument('-v', action='version', version='1.0.1 by Skyler Moore')
    
    options = parser.parse_args()
    
    if options.update == 1:
        if options.nobukkit != 1:
            print "updating the server please wait"
            opener=urllib2.build_opener()
            print "downloading newest server version"
            page=opener.open("http://ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar")
            data=page.read()
            fout=open('craftbukkit-0.0.1-SNAPSHOT.jar', 'wb')
            fout.write(data)
            fout.close()
            page.close()
            print "done, updating"
    
    if options.nobukkit == 1:
        print "running the default server"
        if options.update == 1:
            opener=urllib2.build_opener()
            print "downloading newest default server version"
            page=opener.open("http://www.minecraft.net/download/minecraft_server.jar")
            data=page.read()
            fout=open('minecraft_server.jar', 'wb')
            fout.write(data)
            fout.close()
            page.close()
            print "done updating"
        skipbukkit=1
    
    if options.updateurl != 0:
        print "updating the server from " + options.updateurl
        opener=urllib2.build_opener()
        print "downloading"
        page=opener.open(options.updateurl)
        data=page.read()
        fout=open('craftbukkit-0.0.1-SNAPSHOT.jar', 'wb')
        fout.write(data)
        fout.close()
        page.close()
        print "done updating"
    
    if options.Pdir != 0:
        pluigins="-P " + options.Pdir + " "
    
    if options.cfile != 0:
        config="-c " + options.cfile + " "
    
    if options.host != 0:
        host="-h " + options.host + " "
    
    if options.online != 1:
        online="-o " + options.online + " "
    
    if options.port != 0:
        port="-p " + options.port + " "
    
    if options.size != 0:
        players="-s " + options.size + " "
    
    if options.world != 0:
        world="-w " + options.world + " "
    
    while run == 1:
        if skipbukkit != 1:
            check=glob.glob('craftbukkit-0.0.1-SNAPSHOT.jar')
            if check == []:
                print "Didnt detect craftbukkit, downloading it now"
                opener=urllib2.build_opener()
                print "downloading newest server version"
                page=opener.open("http://ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar")
                data=page.read()
                fout=open('craftbukkit-0.0.1-SNAPSHOT.jar', 'wb')
                fout.write(data)
                fout.close()
                page.close()
                print "done"
    
            os.system('java -Xmx' + maxram + ' -Xms' + minram +' -jar craftbukkit-0.0.1-SNAPSHOT.jar ' + plugins + config + host + online + port + players + world)
        elif skipbukkit == 1:
            check=glob.glob('minecraft_server.jar')
            if check == []:
                print "Didnt detect a default server, downloading it now"
                opener=urllib2.build_opener()
                print "downloading newest server version"
                page=opener.open("http://www.minecraft.net/download/minecraft_server.jar")
                data=page.read()
                fout=open('minecraft_server.jar', 'wb')
                fout.write(data)
                fout.close()
                page.close()
                print "done"
    
            os.system('java -Xmx' + maxram + ' -Xms' + minram +' -jar minecraft_server.jar nogui')
        print ""
        print ""
        print ""
        print "the server stopped, it will auto restart in 5 sec, press \"ctr+C\" to interupt"
        time.sleep(5)    
     
  2. Offline

    NotoriousPyro

  3. Offline

    skyler moore

    Thanks for the link, i'm interested in getting pynecraft to work however i seem to not be able to get it to do anything other than start the server. Is there a place that lists all the commands and how to use them?
     
Thread Status:
Not open for further replies.

Share This Page