【问题标题】:Creating a very simple IRC bot with SSL?使用 SSL 创建一个非常简单的 IRC 机器人?
【发布时间】:2011-06-16 14:53:39
【问题描述】:

我该如何实现 SSL?我一直在尝试使用 SSL 套接字,但我还不能让它工作。有什么想法吗?

谢谢, 丹尼斯

编辑:知道了!

# Import some necessary libraries.
import socket, ssl

# Some basic variables used to configure the bot        
server = "irc.freenode.net" # Server
port = 7000 # Port
channel = "#test" # Channel
botnick = "LOLBOT" # Your bots nick

def ping(): # This is our first function! It will respond to server Pings.
  ircsock.send("PONG :pingis\n")  

def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
  ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n") 

def joinchan(chan): # This function is used to join channels.
  ircsock.send("JOIN "+ chan +"\n")

def hello(): # This function responds to a user that inputs "Hello Mybot"
  ircsock.send("PRIVMSG "+ channel +" :Hello!\n")

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server, port)) # Here we connect to the server using the port 6667
ircsock = ssl.wrap_socket(s)
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot

joinchan(channel) # Join the channel using the functions we previously defined

while 1: # Be careful with these! it might send you to an infinite loop
  ircmsg = ircsock.recv(2048) # receive data from the server
  ircmsg = ircmsg.strip('\n\r') # removing any unnecessary linebreaks.
  print(ircmsg) # Here we print what's coming from the server

  if ircmsg.find(":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
    hello()

  if ircmsg.find("PING :") != -1: # if the server pings us then we've got to respond!
    ping()

【问题讨论】:

  • 我注意到我一直在胡言乱语。基本上我没有对套接字进行 SSL 包装。有人可以给我一个如何正确执行此操作的示例吗?

标签: python sockets ssl openssl


【解决方案1】:

有可能完全错过这个问题,你试过this吗?

【讨论】:

  • 是的,我有,但我更愿意自己尝试、理解和编程机器人 :)
  • 一种非常新颖的方法,祝你好运!
【解决方案2】:

已经有一段时间没有更新了,但是irclib 支持 ssl 套接字。你可以看看它是怎么做的。

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2011-06-15
    • 2011-02-27
    • 2016-07-16
    • 2011-04-18
    • 2016-02-05
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多