BlackJack,
VIELEN DANK für Deine Hilfe
Ja, ich denke mir, dass meine Programmierung recht unsauber ist
aber nach einigem probieren (und leider auch weglassen der Klasse - das GUI wird nun direkt im Hauptprogramm gebiltet) funktioniert die ganze Anwendung so wie ich mir das vorstelle
Bei meinen nächsten Ideen und Umsetzungsversuchen werde ich bestimmt um einiges sauberer sein
Aber hier nun mal der ganze Code:
Mit diesem Programm ist es für Teams des Spiels LFS möglich, einen ban (oder unban) mit einem Klick für mehrere Server auf einmal auszuführen, ohne sich mit jedem einzelnen Server verbinden zu müssen.
Code: Alles auswählen
#
import threading
import Pyinsim
import sys
# Init globals
insim = Pyinsim.InSim(Pyinsim.INSIM_TCP)
connections = {}
players = {}
Action = ''
banaction = ''
Adminpass = ''
# Store hosts and ports as a dictionary (dict).
hosts = {29999: '127.0.0.1', 29998: '127.0.0.1', 29997: '192.168.2.104', 29996: '192.168.2.104'}
# Create a list to store each InSim connection.
sockets = []
# Helper functions.
def SendMessage(msg):
"""Send message to LFS."""
if len(msg) > 64:
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_MSX, Msg=msg))
else:
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_MST, Msg=msg))
# Connection lost.
def ConnectionLost():
print 'InSim connection lost.'
sys.exit(0)
insim.ConnectionLost(ConnectionLost)
#Timer function to close InSim client
def timer_close():
for socket in sockets:
socket.Close()
#Setting ban variables from GUI
def ban():
global Adminpass
Adminpass = adminpass.get()
global Action
Action = 'ban'
UName = lname.get()
if duration.get() == '':
banduration = '1'
else:
banduration = duration.get()
global banaction
banaction = UName + ' ' + banduration
master.destroy()
#Setting unban variables from GUI
def unban():
global Adminpass
Adminpass = adminpass.get()
global Action
Action = 'unban'
global banaction
banaction = lname.get()
master.destroy()
#Tkinter GUI
from Tkinter import *
master = Tk()
master.title('ban/unban tool')
label = Label(master, text='ban/unban tool',
fg='red',
bg='white',
bd=5,
pady=4,
padx=4,
font=('Courier New',18,'bold underline'))
label.pack()
adtxt = Message(master, width = 250,
text='Admin Pass:')
adtxt.pack(side=LEFT)
adminpass = Entry(master)
adminpass.pack(side=LEFT)
lntxt = Message(master, width = 250,
text='Licence name:')
lntxt.pack(side=LEFT)
lname = Entry(master)
lname.pack(side=LEFT)
dutxt = Message(master, width = 250,
text='Duration:')
dutxt.pack(side=LEFT)
duration = Entry(master)
duration.pack(side=LEFT)
banButton = Button(master, text='BAN',
width = 10,
command=ban)
banButton.pack(side=LEFT)
unbanButton = Button(master, text='UNBAN',
width = 10,
command=unban)
unbanButton.pack(side=LEFT)
exitButton = Button(master, text='EXIT',
height = 3,
width = 10,
command=master.destroy)
exitButton.pack(side=TOP)
master.mainloop()
# Loop over the hosts dictionary.
for port, host in hosts.iteritems():
# Create new InSim object.
insim = Pyinsim.InSim()
# Connect to this server.
insim.Connect(host, port)
# Add connected InSim object to our sockets list.
sockets.append(insim)
# Loop over the sockets
for socket in sockets:
if socket.Connected == True:
print '/' + Action + ' ' + banaction + ' executed'
socket.SendP(Pyinsim.Packet(Pyinsim.ISP_ISI, Admin=Adminpass,
IName='^3bantool', ReqI=1))
SendMessage('/' + Action + ' ' + banaction)
# Start timer.
timer = threading.Timer(15, timer_close)
timer.start()
# END OF PROGRAM
for socket in sockets:
socket.Run()