Python IRC bot问题 [英] Python IRC bot question

查看:78
本文介绍了Python IRC bot问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制造一个机器人,让我可以从irc定义函数,也可以像python解释器一样对待我的机器人.

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

我的问题是怎么做

推荐答案

如果您不担心安全性(并且如果这是一个个人项目,那么这是一个有效的假设),那么Python具有多个功能(compileexeceval)可以在此处提供帮助.请注意,Python 2和Python 3之间存在差异,但是以下示例对两者均适用:

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

如果要让其他人使用该机器人,则需要非常注意让别人调用的功能.例如,os模块中的大多数内容都有可能在irc bot上下文中做不良的事情.

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.

这篇关于Python IRC bot问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆