【问题标题】:Sending json data over irc (python3)通过 irc (python3) 发送 json 数据
【发布时间】:2014-09-24 23:26:58
【问题描述】:

在这个项目中,我试图将 json 数据发送到特定的 irc 通道。 irc bot 应该一次做两件事,检查某个消息,然后发送 json 数据(抱歉,经常重复 json 数据:)。

我为函数创建了一个类,该函数在网站上搜索它应该通过 irc (search.py​​) 发送的数据:

import re
import json
import requests


class search:



    def run():

        data = requests.get("http://boards.4chan.org/g/catalog").text
        match = re.match(".*var catalog = (?P<catalog>\{.*\});.*", data)

        if not match:
            print("Couldn't scrape catalog")
            exit(1)

        catalog = json.loads(match.group('catalog'))

        running = True
        while running:

                try:
                    filtertext = ("tox")
                    for number, thread in catalog['threads'].items():
                        sub, teaser = thread['sub'], thread['teaser']
                        if filtertext in sub.lower() or filtertext in teaser.lower():
                            #liste.append(teaser)
                            #return(teaser)

                            #print(liste[0])
                            return(teaser)
                            running = False

应该将数据发送到irc通道的文件(irctest.py):

import socket,threading,time
from search import search

# Some basic variables used to configure the bot        
server = b"irc.freenode.net" # Server
channel = b"#volafile" # Channel
botnick = b"Mybot" # Your bots nick
troo = True

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

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

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

def worker():
  print(threading.currentThread().getName(), 'Starting')
  while True:
      #liste3= search.run()
      #teaser1 = str(search.teaser)
      ircsock.send(b"PRIVMSG "+ channel + b" :"+ search.run() + b"\n")

  print(threading.currentThread().getName(), 'Exiting')


ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))

ircsock.send(b"USER "+ botnick + b" "+ botnick + b" "+ botnick + b" :This bot is a   result of a tutoral covered on http://shellium.org/wiki.\n")
ircsock.send(b"NICK "+ botnick + b"\n")

joinchan(channel) # Join the channel using the functions we previo

time.sleep(10)

w = threading.Thread(name='worker', target=worker) 
i=0
w.start()
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(b'\n\r') # removing any unnecessary linebreaks.
  print(ircmsg) # Here we print what's coming from the server

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

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

但我收到此错误消息:

        Exception in thread worker:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "irctest.py", line 24, in worker
    ircsock.send(b"PRIVMSG "+ channel + b" :"+ bytes(search.run()) + b"\n"\
TypeError: string argument without an encoding

【问题讨论】:

  • 如果您更改代码,您还应该更新您的 Traceback。
  • 错误一模一样。
  • 没关系。目前 Traceback 不适合上面的代码。

标签: python json multithreading python-3.x irc


【解决方案1】:

当您在函数 worker() 中调用 ircsock.send() 时,您的最后一个字符串不是字节字符串 ("\n")。您还应该将search.run() 返回值转换为带有bytes(search.run(), "utf-8") 的字节串。将特定行更改为:

ircsock.send(b"PRIVMSG "+ channel + b" :"+ bytes(search.run(), "utf-8") + b"\n")

【讨论】:

  • 我能做些什么来解决这个问题?
猜你喜欢
  • 2015-04-12
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多