Twitch Viewer机器人程序脚本中出现Python线程错误



我当前正在使用:

Twitch Viewer Bot

并且一直在调整代码以使其正常工作。这就是我目前拥有的:

import requests
import subprocess
import json
import sys
import threading
import time
from multiprocessing import Queue
import urllib3
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
#urllib3.disable_warnings() unverified HTTPS requests
numberOfViewers = int(sys.argv[1])
builderThreads = int(sys.argv[2])
startTime = time.time()
numberOfSockets = 0
concurrent = 25
urls = []
urlsUsed = []
proxies = {
'http': 'http://173.201.183.172:8000',
'http': 'http://94.181.34.64:81',
}

# def getURL(): # Get tokens
#   output = subprocess.Popen(["livestreamer", "twitch.tv/swagvyper", "-j"], stdout=subprocess.PIPE).communicate()[0]
#   return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter
def getURL():
output = urlopen('http://www.twitch.tv/CHANNEL') # Any URL
return json.load(output)['streams']['worst']['url']
def build(): # Builds a set of tokens, aka viewers
global numberOfSockets
global numberOfViewers
while True:
if numberOfSockets < numberOfViewers:
numberOfSockets += 1
print("Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers))
urls.append(getURL())
def view(): # Opens connections to send views
global numberOfSockets
while True:
url=q.get()
# requests.head(url, proxies=proxies)
requests.head(url) # Sending a HEAD request is enough to count as a view
if (url in urlsUsed):
urls.remove(url)
urlsUsed.remove(url)
numberOfSockets -= 1
else:
urlsUsed.append(url)
q.task_done()
if __name__ == '__main__':
for i in range(0, builderThreads):
threading.Thread(target = build).start()
while True:
while (numberOfViewers != numberOfSockets): # Wait until sockets are built
time.sleep(1)
q=Queue(concurrent*2)
for i in range(concurrent):
try:
t=threading.Thread(target=view)
t.daemon=True
t.start()
except:
print('thread error')
try:
for url in urls:
print(url)
q.put(url.strip())
q.join()
except KeyboardInterrupt:
sys.exit(1)

然而,当运行时,我似乎得到了这个错误,我不能100%确定从这里去哪里:

thread errorException in thread Thread-1:
Traceback (most recent call last):
File "C:Program Files (x86)Python36-32libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:Program Files (x86)Python36-32libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "bot.py", line 41, in build
urls.append(getURL())
File "bot.py", line 31, in getURL
output = urlopen('http://www.twitch.tv/dsboywonder25') # Any URL
NameError: name 'urlopen' is not defined
Traceback (most recent call last):
File "bot.py", line 70, in <module>
File "C:Program Files (x86)Python36-32libthreading.py", line 846, in start
MemoryError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "bot.py", line 72, in <module>
MemoryError

我想知道是否有人以前见过这件事,甚至直接向我指出了正确的方向。提前通知您:(

文件"bot.py",第31行,在getURL中output=urlopen('http://www.twitch.tv/dsboywonder25'(#任何URL名称错误:名称"urlopen"未定义

urlopen没有定义:您没有从任何地方声明或导入urlopen,因此脚本不知道您在说什么。

CCD_ 2是urllib/urllib2的一种方法。您已经导入了requestsurllib3,这是另外两个用于发出http请求的模块。您需要决定哪个模块对给定的应用程序最有用,并使用适当的方法打开连接/发出所需的请求。

最新更新