Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用SSL创建一个非常简单的IRC bot?_Python_Sockets_Ssl_Openssl - Fatal编程技术网

Python 使用SSL创建一个非常简单的IRC bot?

Python 使用SSL创建一个非常简单的IRC bot?,python,sockets,ssl,openssl,Python,Sockets,Ssl,Openssl,我如何用它实现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" # Cha

我如何用它实现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套接字。你可以看看它是如何做到的。

是的,我有,但我宁愿自己尝试、理解并编程一个机器人:)一种非常新颖的方法,祝你好运!我注意到我一直在闲逛。基本上,我无法用SSL封装套接字。有人能给我举个例子说明如何正确地做到这一点吗?