Python IRC bot question

依然范特西╮ 提交于 2019-12-01 13:48:26

If you're not worried about security (and if this is a personal project then that's a valid assumption), then Python has several functions (compile, exec, eval) that can help here. Note that there are differences between Python 2 and Python 3, but the following example works for both:

>>> s = "print('hello world')"
>>> code = compile(s, "<string>", "exec")
>>> exec(code)
hello world

If you're going to let other people use this bot, you will want to pay very careful attention to the functions you let people call. For example, most things in the os module have potential to do undesirable things in an irc bot context.

I wrote skybot, which has functionality close to what you want.

<rmmh> .py print "hello %03d, %s" % (5, "blah")
<skybot> Scaevolus: hello 005, blah

The Python interpreter runs on GAE and is a clone of http://shell.appspot.com/ modified to not store state, to prevent people from breaking the bot.

You could hook your code into this: http://tumbolia.appspot.com/py/

import urllib, urllib2
uri = 'http://tumbolia.appspot.com/py/'
response = urllib2.urlopen(uri + urllib.quote("print 'hello world!'")).read()
print response

The nice thing about this solution is that you don't have to worry about the code being executed on your server. This is how the IRC bot phenny executes python code for the '.py' command.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!