Accessing youtube via stem (tor) gives unable to reach url error

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

I have combined this example in stem with pytube to measure the time it takes for me to download a youtube video via Tor.

Here's the code:

import io import pycurl import stem.process from stem.util import term import pickle import socks  # SocksiPy module import socket import urllib from pytube import YouTube import pdb import time import string import random  SOCKS_PORT = 9050 # Set socks proxy and wrap the urllib module  socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT) socket.socket = socks.socksocket  # Perform DNS resolution through the socket  def getaddrinfo(*args):   return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]  socket.getaddrinfo = getaddrinfo  def id_generator(size=6, chars=string.ascii_uppercase + string.digits):     return ''.join(random.choice(chars) for _ in range(size))  def query(url):      try:         yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')         yt.set_filename(id_generator(6))         video = yt.get('3gp', '144p')         start = time.time()         video.download('/tmp/')          end = time.time()         time_ = end - start         return time_      except:         return "Unable to reach %s" % url  def print_bootstrap_lines(line):     if "Bootstrapped " in line:         print(term.format(line, term.Color.BLUE))  def main():      print(term.format("Starting Tor:\n", term.Attr.BOLD))      tor_process = stem.process.launch_tor_with_config(         config = {             'SocksPort': str(SOCKS_PORT),          },         init_msg_handler = print_bootstrap_lines,     )      url = 'https://www.youtube.com/watch?v=5mkm22yO-bs'     times = [0]*10      for i in xrange(10):          times[i] = query(url)      tor_process.kill()  # stops tor      with open('times_yout_jung.pickle', 'wb') as f:         pickle.dump(times, f)  if __name__ == "__main__":     main()

However, it gives the "Unable to reach url" error. And when I try normally route traffic via this code (to see whether pytube is working) then it works:

from pytube import YouTube import pdb import time      import string import random  def id_generator(size=6, chars=string.ascii_uppercase + string.digits):     return ''.join(random.choice(chars) for _ in range(size))  def main():       pdb.set_trace()     yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')     yt.set_filename(id_generator(6))     video = yt.get('3gp', '144p')     start = time.time()     video.download('/tmp/')      end = time.time()      print("Time ", end - start)  if __name__ == "__main__":     main()

I've tried to debug the code with pdb but I can't seem to figure out what could be going wrong. Could anyone please help?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!