2014-01-20 94 views
2

我得到一個錯誤,這個腳本,當我運行它 此外,我要提到我剛開始學習Python的Python的請求包腳本錯誤

import requests 
import subprocess 
import json 
import sys 
import threading 
import time 
from Queue import Queue 

numberOfViewers = int(sys.argv[1]) 
builderThreads = int(sys.argv[2]) 
startTime = time.time() 
numberOfSockets = 0 
concurrent = 25 
urls = [] 
urlsUsed = [] 

def getURL(): # Get tokens 
    output = subprocess.Popen(["livestreamer", "twitch.tv/ispazzcraft", "-j"], stdout=subprocess.PIPE).communicate()[0] 
    return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter 

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) 
     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) 

以下是錯誤

Traceback (most recent call last): 
    File "C:\Python27\Scripts\test.py", line 9, in <module> 
    numberOfViewers = int(sys.argv[1]) 
IndexError: list index out of range 

別人說他們通過安裝請求包來修復它,但我仍然得到這個錯誤 另外我看了,我確實有最新版本的請求包

+0

sys.argv中的參數列表通過命令行提交。如果你得到這個錯誤,這意味着你沒有提供足夠的參數。您需要從命令行運行此腳本,例如:'python script.py 12 19'。 – jayelm

回答

1

你應該提供2命令行參數(數字)使用這個腳本:如果腳本的名字是test.py

numberOfViewers = int(sys.argv[1]) 
builderThreads = int(sys.argv[2]) 

例如:

test.py 1 2