Python IRC 机器人问题

发布于 2024-10-28 20:23:10 字数 253 浏览 11 评论 0原文

我想制作一个机器人,它允许我从 irc 定义函数,并将我的机器人视为 Python 解释器。

例如,我想制作一个可以执行以下操作的机器人:

<mynick> py print "test"
<bot> test
<mynick> define hi(): print "hi"
<mynick> hi()
<bot> hi

我的问题是如何做到这一点

I want to make a bot that will allow me to define functions from irc and treat my bot like a Python interpreter too.

For example, I want to make a bot that can do something like the following:

<mynick> py print "test"
<bot> test
<mynick> define hi(): print "hi"
<mynick> hi()
<bot> hi

MY QUESTION IS HOW TO DO THIS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光与爱终年不遇 2024-11-04 20:23:10

如果您不担心安全性(如果这是一个个人项目,那么这是一个有效的假设),那么 Python 有几个函数(compileexeceval)可以在这里提供帮助。请注意,Python 2 和 Python 3 之间存在差异,但以下示例适用于两者:

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

如果您打算让其他人使用此机器人,您将需要非常仔细地关注您让人们调用的函数。例如,os 模块中的大多数内容都有可能在 irc 机器人上下文中执行不良操作。

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.

我三岁 2024-11-04 20:23:10

我写了 skybot,它的功能接近你想要的。

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

Python 解释器在 GAE 上运行,是 http://shell.appspot.com/ 的克隆,已修改为不存储状态,以防止人们破坏机器人。

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.

挖个坑埋了你 2024-11-04 20:23:10

您可以将代码挂接到以下位置: 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

这个解决方案的优点是您不必担心服务器上执行的代码。这就是 IRC 机器人 phenny 执行“.py”命令的 python 代码的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文