2017-09-18 4 views
0

안녕하세요 여러분, 내 친구들의 채널에서 트위터를 만드는 법을 배우고 있습니다.'str'이 아닌 object와 같은 바이트가 필요합니다.

tmi: :tmi.twitch.tv 001 wasddabulyuasede_bot :Welcome, GLHF! 
:tmi.twitch.tv 002 wasddabulyuasede_bot :Your host is tmi.twitch.tv 

:tmi.twitch.tv 003 wasddabulyuasede_bot :This server is rather new 
:tmi.twitch.tv 004 wasddabulyuasede_bot :- 
:tmi.twitch.tv 375 wasddabulyuasede_bot :- 
:tmi.twitch.tv 372 wasddabulyuasede_bot :You are in a maze of twisty passages, all alike. 
:tmi.twitch.tv 376 wasddabulyuasede_bot :> 

wasddabulyuasede_bot: :[email protected]_bot.tmi.twitch.tv JOIN #wlrs 
_ 
:wasddabulyuasede_bot.tmi.twitch.tv 353 wasddabulyuasede_bot = #wlrs_ :wasddabulyuasede_bot 
:wasddabulyuasede_bot.tmi.twitch.tv 366 wasddabulyuasede_bot #wlrs_ :End of /NAMES list 

wlrs_: swear 

Traceback (most recent call last): 
    File "Bot.py", line 57, in <module> 
    timeout(s,username,10) 
    File "Bot.py", line 33, in timeout 
    chat(sock, ".timeout {}".format(user, secs)) 
    File "Bot.py", line 14, in chat 
    sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg_encoded)) 
TypeError: a bytes-like object is required, not 'str' 
: 로봇은 어떤 몸이 단어를 사용하여 시간 제한을 suppoused 있도록 지금 내가 봇이 대화방에 참여하지만 난 관리 부분을 알아낼 수 있도록 할 수 있었다 대신 내가이 오류가 "맹세" CODE

#cfg.py 
#oauth key has been removed 

HOST = "irc.chat.twitch.tv" 
PORT = 6667 
PASS = "oauth:xxxxxxxxxxxxxxxxxxxx" 
NICK = "wasddabulyuasede_bot" 
CHAN = "#wlrs_" 
RATE = (20/30) 
PATT = [ 
    r"swear" 
] 

Bot.py 

from cfg import HOST, PORT, PASS, NICK, CHAN, RATE, PATT 
import cfg 
import socket 
import re 
import time 

def chat(sock, msg): 
    """ 
    Send a chat message to the server. 
    Keyword arguments: 
    sock -- the socket over which to send the message 
    msg -- the message to be sent 
    """ 
    msg_encoded = msg.encode("utf-8") 
    sock.send("PRIVMSG #{} :{}".format(cfg.CHAN, msg_encoded)) 

# def ban(sock, user): 
#  """ 
#  Ban a user from the current channel. 
#  Keyword arguments: 
#  sock -- the socket over which to send the ban command 
#  user -- the user to be banned 
#  """ 
#  chat(sock, ".ban {}".format(user)) 
# 
def timeout(sock, user, secs=600): 
    """ 
    Time out a user for a set period of time. 
    Keyword arguments: 
    sock -- the socket over which to send the timeout command 
    user -- the user to be timed out 
    secs -- the length of the timeout in seconds (default 600) 
    """ 
    chat(sock, ".timeout {}".format(user, secs)) 


# ----- network functions ----- 
s = socket.socket() 
s.connect((HOST, PORT)) 
s.send("PASS {} \r\n".format(PASS).encode("utf-8")) 
s.send("NICK {} \r\n".format(NICK).encode("utf-8")) 
s.send("JOIN {} \r\n".format(CHAN).encode("utf-8")) 


# pattern we are looking for 
CHAT_MSG=re.compile(r"^:\w+!\[email protected]\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") 

while True: 
    response = s.recv(1024).decode("utf-8") 
    if response == "PING :tmi.twitch.tv\r\n": 
     s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) 
    else: 
     username = re.search(r"\w+", response).group(0) # return the entire match 
     message = CHAT_MSG.sub("", response) 
     print(username + ": " + message) 
     for pattern in cfg.PATT: 
      if re.match(pattern,message): 
       timeout(s,username,10) 
       break 
    time.sleep(1/cfg.RATE) 
+0

[소켓을 통해 (파이썬) 문자열을 보내기]의 사용 가능한 복제 (https://stackoverflow.com/questions/21233340/sending-string-via-socket-python) –

답변

1

스트링은 유니 코드 코드 포인트들의 시퀀스를 나타내는 추상화이다. 문자열을 바이트 시퀀스로 바꾸려면 문자열을 인코딩해야합니다. 예를 들어 와이어에서 텍스트를 나타내는 방법을 결정하십시오.

full_msg = "PRIVMSG #{} :{}".format(cfg.CHAN, msg) 
msg_encoded = full_msg.encode("utf-8") 
sock.send(msg_encoded) 
+0

: 트 위치의 경우, UTF-8을 사용 'socket.send'는 바이트를 필요로하지 않습니까? 여기에 str을주지 않습니까? –

+0

'socket.send'는 바이트를 기대합니다 : https://docs.python.org/3/library/socket.html#socket.socket.send – Felk

+0

예, 첫 줄에는 str에서 bytes로 이동합니다. 하지만 두 번째 줄의 형식이 str이 아닌가? –

관련 문제