多处理和套接字,简化

时间:2017-04-09 16:44:19

标签: python windows python-3.x sockets python-multiprocessing

我试图让我的IRC机器人一次处理多条消息,但它不会发回消息。

行为:Process(target=func)被调用,func()调用其中包含socket.socket().send(message)的函数,但邮件不会发送。怀疑是套接字没有传递给发送函数。

代码:

import socket
import re
import requests
import urllib
import config  # just my file of variables
import math
import time
import sys
import winsound
import string
import random
import multiprocessing
# import traceback

# CONNECTION COMMANDS
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = config.server  # Server
password = config.password  # Password
botnick = config.botnick  # Your bots nick
adminname = config.adminname  # Your IRC nickname
exitcode = config.exitcode
ircsock.settimeout(300)

def connection(host, port, password, nick, realname):
    ircsock.connect((host, port))
    ircsock.send(bytes("PASS " + password + "\n", "UTF-8"))
    ircsock.send(bytes("USER " + botnick + " " + botnick + " " + botnick + " " + botnick + "\n", "UTF-8"))
    ircsock.send(bytes("NICK " + botnick + "\n", "UTF-8"))


def ping():  # respond to server Pings.
    ircsock.send(bytes("PONG :pingis\n", "UTF-8"))
    print("Ponged after " + str(time.time() - last_ping) + " seconds from last ping!")


def sendmsg(msg, target):  # sends messages to the target.
    # it enters here, no problem
    ircsock.send(bytes("PRIVMSG " + target + " :" + msg + "\n", "UTF-8"))  ###  At this point, when using multiprocessing, the bot fails  ###
    print("Sending: [" + str(msg) + "] to: " + str(target))


# MAIN


if __name__ == '__main__':
    connection(server, 6667, password, botnick, botnick)
    # joinchan(channel)
    while 1:
        # print("Connected!")
        ircmsg = ircsock.recv(1024).decode("UTF-8")
        ircmsg = ircmsg.strip('\n\r')
        if ircmsg.find("PRIVMSG") != -1:
            try:
                # “:[Nick]!~[hostname]@[IP Address] PRIVMSG [channel] :[message]”
                name = ircmsg.split('PRIVMSG', 1)[0].split(':')[-1].split("!")[0]  # ircmsg.split('!', 1)[0][1:]
                message = ircmsg.split('PRIVMSG', 1)[1].split(':', 1)[1].splitlines()[0]  # .strip()[0]
                me = ircmsg.split('PRIVMSG', 1)[1].split(':', 1)[0].split()[0]
                # print(me)
                print("name: " + name + ", message: " + message)
                if len(name) < 17:
                    if me == botnick:

                        if message.find("Hi!") != -1:
                            process1 = multiprocessing.Process(target=sendmsg, args=("Hello!", name))
                            process1.daemon = True
                            process1.start()

                        if name.lower() == adminname.lower() and message.rstrip() == exitcode:
                            sendmsg("Bot is quitting.", name)
                            ircsock.send(bytes("QUIT \n", "UTF-8"))
                            sys.exit()

                    time.sleep(1)
            except:
                pass
        elif ircmsg.find("PING") != -1:
            ping()

请尽可能简单地说出你的答案,因为我不熟悉Python。上面的代码可以使用正确的config.py文件运行。

格式:

password = ""  # password to open the server
exitcode = ""  # What is typed to stop the bot
server = ""  # Server
botnick = ""  # Your bots nick
adminname = ""  # Your IRC nickname

0 个答案:

没有答案