RE: Need help with a script 12-18-2012, 11:21 PM
#4
My pleasure,
For commands, look at what your handel connection method is actually doing
For commands, look at what your handel connection method is actually doing
Code:
def joke():
return "I really hate this damned machine\nI really wish they'd sell it\nIt never does quite what I want\nbut only what I tell it!"
def anotherCommand():
file = open("Hello", 'w')
file.write("anotherCommand makes a file!")
file.close()
print("Someone did a command!")
return "You just did a command!"
#Reason number ___ python is awesome: you can make arrays of methods with strings as indexes
commandlist = {}
commandlist["Joke"] = joke #Does the joke method
commandlist["AnotherCommand"] = anotherCommand #Does the anotherCommand method
def clientHandel(conn, addr)
data = conn.recv(1024) #This waits till the connection has sent something, and stores it in data
string = str(data, 'UTF-8') #Python sends bytes over, so we need to convert it to a string
try:
message = commandlist[string]()
except:
message = 'Failed to do command!'
outputBytes = bytes(message, 'UTF-8') # need to turn it back into bytes before sending it
conn.send(outputBytes) #Sends a message to the client