
Geschrieben von
emerge
am 10.02.2009 um 16:45 Uhr.
Beiträge: 91 /
#1760
Kontakt:
Mailen
|
WWW
|
ICQ
Hallo.
Hier mal ein simples Beispiel, wie man mit mit dem "socket" Modul einen IRC Bot schreibt.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
" skeletonIRC - a simple IRC skeleton in python "
" coded by duke <http://botnetz.com/> "
" released under GNU/GPLv2, use it ;) "
# Config
iServer = "irc.skelettor.com"
iPort = 6667
iNick = "Skelettor"
iChannel = "#skelettor"
iMessage = "skeletonIRC - a simple IRC skeleton in python!"
# Importing needed modules
import socket, sys, string, time
# IRC class
class irc:
def __init__(self):
# Creating socket and buffer, also set connected to 0
self.s = socket.socket()
self.rbuf = ''
self.connected = 0
def connect(self, host, port, spass, nick, user, real):
try:
# try to connect to server > host:port, pass, nick, user
self.s.connect((host, port))
self.s.send("PASS %s\n" % (spass))
self.s.send("NICK %s\n" % (nick))
self.s.send("USER %s 0 0 :%s\n" % (user, real))
except:
# if error > exit program
sys.exit("Connection Error.")
# set connection state to 1
self.connected = 1
def get_constate(self):
# return the current connection state
return self.connected
def command(self, command):
# execute a command, already eascaped
self.s.send("%s\n" % (command))
def check(self):
# reading the server responses
self.rbuf=self.rbuf+self.s.recv(1024)
# split the responses
self.tmp=string.split(self.rbuf, "\n")
# delete the last item
self.rbuf=self.tmp.pop()
for self.current in self.tmp:
# strip from "\r"
self.current=string.rstrip(self.current)
# split the string to a list
self.current=string.split(self.current)
# output the array, just for debug reasons
print self.current
# if first list element is "PING", answer with a PONG Response
if self.current[0] == "PING":
# ['PING', ':CB8ABAA7']
self.command("PONG %s" % (self.current[1]))
# 001: Connected to IRC Network
if self.current[1] == "001":
# if fully connected, join the given channel
self.command("JOIN %s" % (iChannel))
# wait a second
time.sleep(1)
# output the given message in the channel
self.command("PRIVMSG %s :%s" % (iChannel, iMessage))
# wait 2 seconds
time.sleep(2)
# quit
self.command("QUIT :get it at http://botnetz.com/")
# close the socket
self.s.close()
# exit the program
sys.exit("Terminated")
# Main Function
def main():
# create the IRC object
sock = irc()
# connect to the Network
sock.connect(iServer, iPort, "", iNick, iNick, iNick)
# while connected to the network...
while sock.get_constate():
# ...check for new server responses
sock.check()
# Call the Main Function
if __name__ == "__main__":
main()