聊天机器人崩溃没有任何错误消息[Python]

时间:2014-08-09 11:33:25

标签: python networking python-3.x httprequest chat

我目前正在开发一个omegle聊天机器人,每次有人响应时都会发出随机问答,并在陌生人断开连接时重新连接。

这是我的代码:

import requests
import sys
import json
import urllib
import random
import time

server = "odo-bucket.omegle.com"

debug_log = open("debug.log","a")
config = {'verbose': open("/dev/null","w")}
headers = {}
headers['Referer'] = 'http://odo-bucket.omegle.com/'
headers['Connection'] = 'keep-alive'
headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2'
headers['Content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
headers['Accept'] = 'application/json'
headers['Accept-Encoding'] = 'gzip,deflate,sdch'
headers['Accept-Language'] = 'en-US'
headers['Accept-Charset'] = 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'

if debug_log:
    config['verbose'] = debug_log

def debug(msg):
    if debug_log:
        print("DEBUG: " + str(msg))
        debug_log.write(str(msg) + "\n")

def getcookies():
    r = requests.get("http://" + server + "/")
    debug(r.cookies)
    return(r.cookies)

def start():
    r = requests.request("POST", "http://" + server + "/start?rcs=1&spid=", data=b"rcs=1&spid=", headers=headers)
    omegle_id = r.content.strip(b"\"")
    print("Got ID: " + str(omegle_id))
    cookies = getcookies()
    event(omegle_id, cookies)

def send(omegle_id, cookies, msg):
    r = requests.request("POST","http://" + server + "/send", data=b"msg=" + urllib.parse.quote_plus(msg).encode('utf-8') + b"&id=" + omegle_id, headers=headers, cookies=cookies)

    if r.content == b"win":
        print("You: " + msg)
    else:
        print("Error sending message, check the log")
        debug(r.content)

def event(omegle_id, cookies):
    captcha = False
    next = False
    r = requests.request("POST","http://" + server + "/events",data=b"id=" + omegle_id, cookies=cookies, headers=headers)

    parsed = json.loads(r.content.decode('utf-8'))
    for e in parsed:
        if e[0] == "waiting":
            print("Waiting for a connection...")
        elif e[0] == "count":
            print("There are " + str(e[1]) + " people connected to Omegle")
        elif e[0] == "connected":
            print("Connection established!")
            send(omegle_id, cookies, "HI I just want to talk ;_;")
        elif e[0] == "typing":
            print("Stranger is typing...")
        elif e[0] == "stoppedTyping":
            print ("Stranger stopped typing")
        elif e[0] == "gotMessage":
            print("Stranger: " + e[1])

            cat=""
            time.sleep(random.randint(1,5))
            i_r=random.randint(1,8)
            if i_r==1:
                cat="that's cute :3"
            elif i_r==2:
                cat="yeah, guess your right.."
            elif i_r==3:
                cat="yeah, tell me something about yourself!!"
            elif i_r==4:
                cat="what's up"
            elif i_r==5:
                cat="me too"
            else:
                time.sleep(random.randint(3,9))
                send(omegle_id, cookies, "I really have to tell you something...")
                time.sleep(random.randint(3,9))
                cat="I love you."

            send(omegle_id, cookies, cat)


        elif e[0] == "strangerDisconnected":
            print("Stranger Disconnected")
            next = True
        elif e[0] == "suggestSpyee":
            print ("Omegle thinks you should be a spy. Fuck omegle.")
        elif e[0] == "recaptchaRequired":
            print("Omegle think's you're a bot (now where would it get a silly idea like that?). Fuckin omegle. Recaptcha code: " + e[1])
            captcha = True



start()

输出:

Got ID: b'shard2:6jcon9xl2s8q3oktmncgn3yh3mylyy'
DEBUG: <<class 'requests.cookies.RequestsCookieJar'>[]>
Waiting for a connection...
Connection established!
You: HI I just want to talk ;_;

我的debug.log:

Got ID: shard2:94x98i6mcspklmjacj365a3oxfqiyb
Got ID: shard2:uc31zvsl7nenfqpgaomidlalu3tvwp
Got ID: shard2:gwa2vrjxif1l6pto2dh3gpifpsptep
Got ID: shard2:kwdanqg5k82h06p14bsgt6xmx8larf
<<class 'requests.cookies.RequestsCookieJar'>[]>
Got ID: shard2:bw9kghhvo5sslb98nryefu58136uta
<<class 'requests.cookies.RequestsCookieJar'>[]>
Got ID: shard2:tn4x0fkvu9mid7p2jrjlakqmtqqcvw
<<class 'requests.cookies.RequestsCookieJar'>[]>
Got ID: shard2:d5emji3hhhdhml7pdlj5nuk6vavnlq
<<class 'requests.cookies.RequestsCookieJar'>[]>
Send errors!
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
b'win'
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
b'win'
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
b'win'
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>
<<class 'requests.cookies.RequestsCookieJar'>[]>

机器人应该保持和谈话,如果有人断开连接,重新连接,但为什么它在发送消息后立即崩溃?

1 个答案:

答案 0 :(得分:1)

从快速看,你没有循环,因此脚本不会崩溃,它只是完成。