2

I am getting an error with this script when I run it Also I should mention I just started learning 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)

Here is the error

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

Someone else said they fixed it by installing requests package and I did but I am still getting this error Also I looked and I do have the latest version of the requests package

1
  • sys.argv is a list of arguments submitted via the command line. If you're getting this error it means you didn't supply enough arguments. You need to run this script from the command line, as so: python script.py 12 19 for example. Commented Jan 20, 2014 at 21:05

1 Answer 1

1

You should provide 2 command line parameters (numbers) to use this script:

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

for example, if name of script is test.py:

test.py 1 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.