Example #1
0
	def __request__(self, path, params={}, do_cache = True):
		cache_path = 'http://api.tv.zing.vn/3.0/%s?%s' %(path, urllib.urlencode(params))
		cached_result = cache.get(cache_path)

		if do_cache == True and cached_result <> None and cached_result <> '':
			return eval(cached_result)

		sparams = params.items() + {'os': 'android', 'app_version': 203, 'api_key': '7d3343b073f9fb9ec75e53335111dcc1', 'list_type': 'new', 'count': self.COUNT}.items()

		url = "http://api.tv.zing.vn/3.0/%s?%s" %(path, urllib.urlencode(sparams))
		proxy = None
		http_proxy = settings.getSetting('proxy')
		if http_proxy <> None and http_proxy <> '':
			proxy_username = settings.getSetting('proxy_username')
			proxy_password = settings.getSetting('proxy_password')
			if proxy_username <> None and proxy_username <> '':
				proxy = {'http': 'http://%s:%s@%s' %(urllib.quote(proxy_username), urllib.quote(proxy_password), http_proxy)}
			else:
				proxy = {'http': 'http://' + http_proxy }

		result = HTTP.Retrieve(url, proxy=proxy)
		result = json.loads(result)
		if do_cache == True:
			cache.set(cache_path, repr(result))
		return result
Example #2
0
def DictFromURL(url):
  """
    Return a dictionary by parsing the YAML response from a given URL.
    
    @param url: The URL to request data from
    @type url: string
    @return: dictionary
  """
  return DictFromString(HTTP.Get(str))
Example #3
0
def DictFromURL(url, encoding="utf8"):
    """
    Return a dictionary by parsing the JSON response with the given encoding from a given URL.
    
    @param url: The URL to request data from
    @type url: string
    @return: dictionary
  """
    return DictFromString(HTTP.Get(url), encoding)
Example #4
0
def ElementFromURL(url,
                   isHTML=False,
                   values=None,
                   headers={},
                   cacheTime=None,
                   autoUpdate=False,
                   encoding=None,
                   errors=None):
    return ElementFromString(
        HTTP.Request(url, values, headers, cacheTime, autoUpdate, encoding,
                     errors), isHTML)
Example #5
0
def FeedFromURL(url,
                values=None,
                headers={},
                cacheTime=None,
                autoUpdate=False):
    return FeedFromString(
        HTTP.Request(url,
                     values=values,
                     headers=headers,
                     cacheTime=cacheTime,
                     autoUpdate=autoUpdate))
Example #6
0
def get_feed(url, post_data=None):
    try:
        headers = {'Referer': 'http://download.hulu.com/hulu10.html'}
        if not post_data:
            action = "GET"
            Log.Debug("Sending HTTP GET to '{}', Headers: {}".format(
                url, headers))
            response = HTTP.Request(url, headers=headers).content
        else:
            action = "POST"
            Log.Debug(
                "Sending HTTP POST to '{}', Headers: {}, Body: '{}'".format(
                    url, headers, post_data))
            response = HTTP.Request(url, headers=headers,
                                    data=post_data).content
    except Ex.HTTPError:
        Log.Exception("Failed to perform action {} on URL: '{}'".format(
            action, url))
    Log.Debug("Got response '%s'", response)
    return response
Example #7
0
    def __request__(self, resource, query_params={}):
        url = 'https://api-v2.hdviet.com/%s' % resource
        if len(query_params) > 0:
            url = '%s?%s' % (url, urllib.urlencode(query_params))

        cached_result = cache.get(url)
        if cached_result <> None and cached_result <> '':
            return json.loads(cached_result)

        result = HTTP.Retrieve(url)
        cache.set(url, result)
        return json.loads(result)
Example #8
0
 def __call__(self, environ, start_response):
   request = webob.Request(environ)
   
   if request.path_info == '/HTTP':
     application = HTTP.Application()
     return application(environ, start_response)
   else:
     if request.path_info == '/HTTPS':
       application = HTTPS.Application()
       return application(environ, start_response)
   
   response = webob.Response(None, 200, None, None)
   return response(environ, start_response)
Example #9
0
    def convert(self, ip, raw_port, transcoding_port, path="."):
        '''
        convertor
        '''
        path = "/etc/enigma2/"
        data = HTTP.Client().get(ip, 80, "/web/getallservices")
        handler = E2WebIFBouquetReader()
        xml.sax.parseString(
            data[data.find("<?xml"):data.find("</e2servicelistrecursive>") +
                 25], handler)
        bouquet_list = codecs.open(path + "bouquets.tv", "w", "utf-8")
        bouquet_list.write("#NAME Bouquets (TV)" + "\n")
        bouquet_list.write(
            "#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet"
            + "\n")

        for bouquet in handler.getBouquets():
            chars_to_remove = ['.', '!', '?', ' ', '(', ')']
            bouquet_name = ""

            for char in bouquet.getName().lower():
                if not (char in chars_to_remove):
                    bouquet_name += char
                else:
                    bouquet_name += "_"

            bouquet_file_name = "userbouquet." + bouquet_name + ".tv"
            bouquet_list.write("#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"" +
                               bouquet_file_name + "\" ORDER BY bouquet" +
                               "\n")

            bouquet_channel_list = codecs.open(path + bouquet_file_name, "w",
                                               "utf-8")
            bouquet_channel_list.write("#NAME " + bouquet.getName() + "\n")

            for channel in bouquet.getChannels():
                if channel.isHD():
                    port = transcoding_port
                else:
                    port = raw_port

                bouquet_channel_list.write(
                    "#SERVICE 4097:0:1:0:0:0:0:0:0:0:" +
                    urllib.quote("http://" + ip + ":" + port + "/" +
                                 channel.getRefernce()) + ":" +
                    channel.getName() + "\n")
                bouquet_channel_list.write("#DESCRIPTION " +
                                           channel.getName() + "\n")

        bouquet_channel_list.close()
        bouquet_list.close()
Example #10
0
def ObjectFromURL(url,
                  values=None,
                  headers={},
                  cacheTime=None,
                  autoUpdate=False,
                  encoding=None,
                  errors=None):
    return ObjectFromString(
        HTTP.Request(url,
                     values=values,
                     headers=headers,
                     cacheTime=cacheTime,
                     autoUpdate=autoUpdate,
                     encoding=encoding,
                     errors=errors))
Example #11
0
def getElementFromHttpRequest(url, encoding, userAgent=USER_AGENT):
    """ Fetches a given URL and returns it as an element.
        Функция преобразования html-кода в xml-код.
    """
    for i in range(3):
        errorCount = 0
        try:
            req = HTTPRequest(url,
                              headers={
                                  'User-agent': userAgent,
                                  'Accept': 'text/html'
                              })
            response = HTTP().fetch(req)
            return str(response.body).decode(encoding)
        except:
            errorCount += 1
            Log.Debug('Error fetching URL: "%s".' % url)
            time.sleep(1 + errorCount)
    return None
Example #12
0
    def __init__(self, root, http_engine=None, crawlerFile=None):
        self.h = http_engine
        if root.startswith("-"):
            print(_("First argument must be the root url !"))
            sys.exit(0)
        if not "://" in root:
            root = "http://" + root
        if (self.__checklink(root)):
            print(_("Invalid protocol: {0}").format(root.split("://")[0]))
            sys.exit(0)
        if root[-1] != "/" and not "/" in root.split("://")[1]:
            root += "/"

        server = (root.split("://")[1]).split("/")[0]
        self.root = HTTP.HTTPResource(root)  # Initial URL
        self.server = server  # Domain
        self.scopeURL = root  # Scope of the analysis

        self.tobrowse.append(self.root)
        self.persister = CrawlerPersister()
Example #13
0
def post_API(action, parameters, secure):
    if secure == True:
        url = 'https://secure.'
    elif secure == False:
        url = 'http://www.'

    url += 'hulu.com/api/1.0/' + action
    parameters['app'] = 'f8aa99ec5c28937cf3177087d149a96b5a5efeeb'
    parameters['sig'] = generate_signature(action, parameters)
    headers = {'Referer': 'http://download.hulu.com/huludesktop.swf?ver=0.1.0'}
    Log.Debug(
        "Sending HTTP POST to {url}, Headers: {headers}, Body: {body}".format(
            url=url, headers=headers, body=urllib.urlencode(parameters)))
    try:
        response = HTTP.Request(url, headers=headers,
                                values=parameters).content
        Log.Debug("Got response " + str(response))
        return response

    except Ex.HTTPError:
        Log.Exception("Failed to perform action " + action)
Example #14
0
def try_req_exc(url):
    global timeout_counter
    try:
        r = HTTP.Http(url, timeout)  # send request
        r.raise_for_status()  # raise HTTPError, if one occurred
        timeout_counter = 0  # restarting timeout_counter for next request
        return r
    except exceptions.HTTPError as err1:
        logging.error(str(err1))
        sys.exit()
    except exceptions.ConnectionError as err2:
        logging.error(str(err2))
        sys.exit()
    except exceptions.Timeout as err3:
        timeout_counter += 1
        logging.error(str(err3))
        if timeout_counter == 3:  # after 3 successive timeouts end streaming ! <----------
            sys.exit()
        return try_req_exc(url)
    except exceptions.RequestException as err4:
        logging.error(str(err4))
        sys.exit()
Example #15
0
def ElementFromURL(url, use_html_parser=False):
    """
    Creates a new Element object with the response from the given URL.
    
    @param url: The URL used in the request
    @type url: string
    @param use_html_parser: Specifies whether to parse the response as HTML instead of XML
    @type use_html_parser: boolean
    @return: Element
  """
    text = HTTP.Get(url)
    if text is not None:
        Log.Add('(Framework) Request to %s return %d bytes' % (url, len(text)))
        if use_html_parser:
            try:
                root = html.fromstring(text)
                test = html.tostring(root, encoding=unicode)
                return root
            except:
                return fromstring(text)
        else:
            return etree.fromstring(text)
    else:
        return None
def __run(_bundlePath):
  #
  # Initializes the framework, verifies the plug-in & extracts information, then enters a
  # run loop for handling requests. 
  #
  global Identifier
  global Debug
  global __bundlePath  
  global __pluginModule
  global __logFilePath
  global __requestHandlers
  global LastPrefix
  
  FirstRun = False
  random.seed()
  
  # Set up the support file paths
  if sys.platform == "win32":
    if 'PLEXLOCALAPPDATA' in os.environ:
      key = 'PLEXLOCALAPPDATA'
    else:
      key = 'LOCALAPPDATA'
    pmsPath = os.path.join(os.environ[key], 'Plex Media Server')
    logFilesPath = os.path.join(os.environ[key], "Plex Media Server", "Logs", "PMS Plugin Logs")
  else:
    pmsPath = "%s/Library/Application Support/Plex Media Server" % os.environ["HOME"]
    logFilesPath = "%s/Library/Logs/PMS Plugin Logs" % os.environ["HOME"]
  
  supportFilesPath = "%s/Plug-in Support" % pmsPath
  frameworkSupportFilesPath = "%s/Framework Support" % pmsPath
  
  # Make sure framework directories exist
  def checkpath(path):
    try:
      if not os.path.exists(path): os.makedirs(path)
    except:
      pass
  
  checkpath("%s/Preferences" % supportFilesPath)
  checkpath("%s/Databases" % supportFilesPath)
  checkpath(logFilesPath)
  checkpath(frameworkSupportFilesPath)
    
  # Set the bundle path
  __bundlePath = _bundlePath.rstrip('/')
  
  # Add the bundle path to the system path, including any libraries
  if os.path.isdir("%s/Contents" % __bundlePath):
    sys.path.append("%s/Contents" % __bundlePath)
    if os.path.isdir("%s/Contents/Libraries" % __bundlePath):
      sys.path.append("%s/Contents/Libraries" % __bundlePath)
  else:
    print "Couldn't find bundle directory"
    return None
  
  # Open the Info.plist file
  f = open("%s/Contents/Info.plist" % __bundlePath, "r")
  infoplist = XML.ElementFromString(f.read())
  f.close()
  if infoplist is None:
    print "Couldn't load Info.plist file from plug-in"
    return

  # Get the plug-in identifier
  Identifier = infoplist.xpath('//key[text()="CFBundleIdentifier"]//following-sibling::string/text()')[0]
  if Identifier is None:
    print "Invalid Info.plist file in plug-in"
    return None
    
  # Set up the log file
  __logFilePath = "%s/%s.log" % (logFilesPath, Identifier)
  if os.path.exists(__logFilePath):
    if os.path.exists("%s.old" % __logFilePath):
      os.remove("%s.old" % __logFilePath)
    os.rename(__logFilePath, "%s.old" % __logFilePath)
  
  # Now we can start logging
  PMS.Log("(Framework) Bundle verification complete", False)
  
  # Check whether debugging is enabled
  try:
    _debug = infoplist.xpath('//key[text()="PlexPluginDebug"]//following-sibling::string/text()')[0]
    if _debug == "1":
      Debug = True
      PMS.Log("(Framework) Debugging is enabled")
  except: pass

  # Log the system encoding (set during bootstrap)
  PMS.Log("(Framework) Default encoding is " + sys.getdefaultencoding())

  # Set up framework paths
  Prefs.__prefsPath = "%s/Preferences/%s.xml" % (supportFilesPath, Identifier)
  Data.__dataPath = "%s/Data/%s" % (supportFilesPath, Identifier)
  Data.__dataItemPath = "%s/DataItems" % Data.__dataPath
  if not os.path.isdir(Data.__dataItemPath):
    FirstRun = True
    os.makedirs(Data.__dataItemPath)
  Resource.__resourcePath = "%s/Contents/Resources" % __bundlePath
  Helper.__helperPath = "%s/Contents/Helpers" % __bundlePath
  Resource.__sharedResourcePath = "%s/Plug-ins/Framework.bundle/Contents/Resources/Versions/1/Resources" % pmsPath
  Database.__databasePath = "%s/Databases/%s.db" % (supportFilesPath, Identifier)
  os.chdir(Data.__dataItemPath)
  Locale.SetDefaultLocale()
  PMS.Log("(Framework) Configured framework modules")

  # Attempt to import the plug-in module - if debugging is enabled, don't catch exceptions
  if Debug:
    import Code as _plugin
    PMS.Log("(Framework) Imported plug-in module")
  else:
    try:
      import Code as _plugin
      PMS.Log("(Framework) Imported plug-in module")
    except ImportError:
      PMS.Log("(Framework) Couldn't import plug-in from bundle")
      __exit()
      return
      
  # Load the list of trusted plug-ins
  _trusted = []
  try:
    _trustedJSON = Resource.LoadShared("trust.json")
    if _trustedJSON:
      _trusted = JSON.ObjectFromString(_trustedJSON)
  except:
    pass

  # Populate the permission lists
  __setupPermissionLists()

  # Register the plug-in with the framework
  __pluginModule = _plugin

  # Check the imported module to make sure nothing untoward is happening!
  if Identifier in _trusted:
    PMS.Log("(Framework) Plug-in is trusted, skipping module check")
  else:
    __scanModules()
    _allowed = []
    for n in PMS.__dict__:
      if n[0] != "_":
        if type(PMS.__dict__[n]).__name__ == "module":
          _allowed.append(n)
    for n in __modWhitelist:
      _allowed.append(n)
    __checkModule(_plugin, _allowed)
    PMS.Log("(Framework) Checked module imports")

  # Initialize the framework modules
  Dict.__load()
  if not FirstRun:
    __checkFrameworkCompatibility()
  Prefs.__load()
  HTTP.__loadCookieJar()
  HTTP.__loadCache()
  if HTTP_TIMEOUT_VAR_NAME in os.environ:
    HTTP.SetTimeout(float(os.environ[HTTP_TIMEOUT_VAR_NAME]))
  else:
    HTTP.SetTimeout(HTTP_DEFAULT_TIMEOUT)
  PMS.Log("(Framework) Initialized framework modules")

  # Call the plug-in's Start method
  PMS.Log("(Framework) Attempting to start the plug-in...")
  __call(__pluginModule.Start)
  PMS.Log("(Framework) Plug-in started", False)

  # Start timers
  __startCacheManager(firstRun=FirstRun)
  
  PMS.Log("(Framework) Entering run loop")
  # Enter a run loop to handle requests
  while True:
    try:
      # Read the input
      path = raw_input()
      path = path.lstrip("GET ").strip()
      LastPrefix = None
      
      # Read headers
      headers = {}
      stop = False
      while stop == False:
        line = raw_input()
        if len(line) == 1:
          stop = True
        else:
          split = string.split(line.strip(), ":", maxsplit=1)
          if len(split) == 2:
            headers[split[0].strip()] = split[1].strip()

      # Set the locale
      if headers.has_key("X-Plex-Language"):
        loc = headers["X-Plex-Language"].lower()
        Locale.__loadLocale(loc)
        
      # Set the version
      if headers.has_key("X-Plex-Version"):
        Client.__setVersion(headers["X-Plex-Version"])

      PMS.Request.Headers.clear()
      PMS.Request.Headers.update(headers)
      
      req = urlparse.urlparse(path)
      path = req.path
      qs_args = cgi.parse_qs(req.query)
      kwargs = {}
      if 'function_args' in qs_args:
        try:
          unquoted_args = urllib.unquote(qs_args['function_args'][0])
          decoded_args = PMS.String.Decode(unquoted_args)
          kwargs = cerealizer.loads(decoded_args)
        except:
          PMS.Log(PMS.Plugin.Traceback())
          raise Exception("Unable to deserialize arguments")
      
      for arg_name in qs_args:
        if arg_name != 'function_args':
          kwargs[arg_name] = qs_args[arg_name][0]
      
      # First, try to match a connected route
      rpath = path
      for key in __prefixHandlers:
        if rpath.count(key, 0, len(key)) == 1:
          rpath = rpath[len(key):]
          break
      f, route_kwargs = MatchRoute(rpath)
      
      if f is not None:
        PMS.Log("(Framework) Handling route request :  %s" % path, False)
        route_kwargs.update(kwargs)
        result = f(**route_kwargs)

      # No route, fall back to other path handling methods
      else:
        mpath = path
        
        if mpath[-1] == "/":
          mpath = mpath[:-1]
        
        # Split the path into components and decode.
        pathNouns = path.split('/')
        pathNouns = [urllib.unquote(p) for p in pathNouns]
      
        # If no input was given, return an error
        if len(pathNouns) <= 1:
          __return("%s\r\n\r\n" % PMS.Error['BadRequest'])
        
        # Otherwise, attempt to handle the request
        else:
          result = None
          pathNouns.pop(0)
          count = len(pathNouns)
          if pathNouns[-1] == "":
            pathNouns.pop(len(pathNouns)-1)
          PMS.Log("(Framework) Handling request :  %s" % path, False)
        
          # Check for a management request
          if pathNouns[0] == ":":
            result = __handlePMSRequest(pathNouns, path, **kwargs)

          else:  
            handler = None
            isPrefixHandler = False
          
            # See if there's a prefix handler available
            for key in __prefixHandlers:
              if mpath.count(key, 0, len(key)) == 1:
                LastPrefix = key
            if mpath in __prefixHandlers:
              handler = __prefixHandlers[mpath]["handler"]
              isPrefixHandler = True
            
            else:
              # Check each request handler to see if it handles the current prefix
              popped = False
              for key in __requestHandlers:
                if handler is None:
                  if path.count(key, 0, len(key)) == 1:
                    # Remove the prefix from the path
                    keyNounCount = len(key.split('/')) - 1
                    for i in range(keyNounCount):
                      pathNouns.pop(0)
                    count = count - keyNounCount
                    # Find the request handler
                    handler = __requestHandlers[key]["handler"]
                    LastPrefix = key
                    popped = True
            
              # If no path request handler was found, make sure we still pop the prefix so internal requests work
              for key in __prefixHandlers:
                if popped == False:
                  if mpath.count(key, 0, len(key)) == 1:
                    keyNounCount = len(key.split('/')) - 1
                    for i in range(keyNounCount):
                      pathNouns.pop(0)
                    popped = True

            # Check whether we should handle the request internally
            handled = False
            if count > 0:
              if pathNouns[0] == ":":
                handled = True
                result = __handleInternalRequest(pathNouns, path, **kwargs)
    
          
            # Check if the App Store has flagged the plug-in as broken
            if os.path.exists(os.path.join(frameworkSupportFilesPath, "%s.broken" % Identifier)):
              #TODO: Localise this bit, use message from the App Store if available
              handled = True
              result = PMS.Objects.MessageContainer("Please try again later", "This plug-in is currently unavailable")
              PMS.Log("(Framework) Plug-in is flagged as broken")
          
            # If the request hasn't been handled, and we have a valid request handler, call it
            else:
              if not handled and handler is not None:
                if isPrefixHandler:
                  result = handler(**kwargs)
                else:
                  result = handler(pathNouns, path, **kwargs)
        
      response = None
      
      # If the request wasn't handled, return an error
      if result == None:
        PMS.Log("(Framework) Request not handled by plug-in", False)
        response = "%s\r\n\r\n" % PMS.Error['NotFound']
        
      # If the plugin returned an error, return it to PMS
      elif result in PMS.Error.values():
        PMS.Log("(Framework) Plug-in returned an error :  %s" % result, False)
        response = "%s\r\n\r\n" % result
        
      # Otherwise, check if a valid object was returned, and return the result
      elif __objectManager.ObjectHasBase(result, Objects.Object):
        resultStr = result.Content()
        resultStatus = result.Status()
        resultHeaders = result.Headers()
      
      elif isinstance(result, basestring):
        resultStr = result
        resultStatus = '200 OK'
        resultHeaders = "Content-type: text/plain\r\n"
      
      if resultStr is not None:
        PMS.Log("(Framework) Response OK")
        resultLen = len(resultStr)
        if resultLen > 0:
          resultHeaders += "Content-Length: %i\r\n" % resultLen
          resultStr = "\r\n"+resultStr
        
      else:
        resultStr = ""
        resultStatus = '500 Internal Server Error'
        resultHeaders = ''
        PMS.Log("(Framework) Unknown response type")
        
      if response == None:
        response = str("%s\r\n%s" % (resultStatus, resultHeaders)) + str(resultStr) + str("\r\n")
      
      #PMS.Log("\n---\n"+response+"---\n")
        
      __return(response)
    
    # If a KeyboardInterrupt (SIGINT) is raised, stop the plugin
    except KeyboardInterrupt:
      # Save data & exit
      __saveData()
      __exit()     
    
    except EOFError:
      # Save data & exit
      __saveData()
      __exit()
          
    # If another exception is raised, deal with the problem
    except:
      __except()
      __return("%s\r\n\r\n" % PMS.Error['InternalError'])
    
    # Make sure the plugin's data is saved
    finally:
      __saveData()
def __triggerAutoHTTPCacheUpdate():
  HTTP.__autoUpdateCachedPages()
  Thread.CreateTimer(HTTP.__autoUpdateCacheTime, __triggerAutoHTTPCacheUpdate)
        #retransmit
        syn_packet = PACKET.getSYN(SOURCE_IP, DESTINATION_IP, PACKET_ID, SOURCE_PORT, DESTINATION_PORT, SEQ_NUM, ACK_NUM, "", WINDOW_SIZE)
        sender_socket.sendto(syn_packet, (DESTINATION_IP, DESTINATION_PORT))
        #Update sending time
        packet_sent_time = time.time()

        #Resent CWND
        CWND = 1


#Send an ACK to the Server
ack_packet = PACKET.getACK(SOURCE_IP, DESTINATION_IP, PACKET_ID, SOURCE_PORT, DESTINATION_PORT, SEQ_NUM, ACK_NUM, "", WINDOW_SIZE)
sender_socket.sendto(ack_packet, (DESTINATION_IP, DESTINATION_PORT))

#SEND HTTP PACKET
request = PACKET.getHTTP(SOURCE_IP, DESTINATION_IP, PACKET_ID, SOURCE_PORT, DESTINATION_PORT, SEQ_NUM, ACK_NUM, HTTP.createHTTPHeader(PATH, HOST), WINDOW_SIZE)
sender_socket.sendto(request, (DESTINATION_IP, DESTINATION_PORT))
#Time when the packet was sent
last_packet_sent_time = time.time()

#Listen for all incoming data packets
transaction_complete = False
last_packet = request

while True:
    try:
        packet = receiver_socket.recvfrom(65565)
        packet = packet[0]
	print len(packet)
        validation_status, faulty_packet = validatePacket(packet)
Example #19
0
def __triggerAutoHTTPCacheUpdate():
    HTTP.__autoUpdateCachedPages()
    Thread.CreateTimer(HTTP.__autoUpdateCacheTime,
                       __triggerAutoHTTPCacheUpdate)
Example #20
0
def __run(_bundlePath):
    #
    # Initializes the framework, verifies the plug-in & extracts information, then enters a
    # run loop for handling requests.
    #
    global Identifier
    global Debug
    global __bundlePath
    global __pluginModule
    global __logFilePath
    global __requestHandlers
    global LastPrefix

    FirstRun = False
    random.seed()

    # Set up the support file paths
    pmsPath = "%s/Library/Application Support/Plex Media Server" % os.environ[
        "HOME"]
    supportFilesPath = "%s/Plug-in Support" % pmsPath
    frameworkSupportFilesPath = "%s/Framework Support" % pmsPath
    logFilesPath = "%s/Library/Logs/PMS Plugin Logs" % os.environ["HOME"]

    # Make sure framework directories exist
    def checkpath(path):
        try:
            if not os.path.exists(path): os.makedirs(path)
        except:
            pass

    checkpath("%s/Preferences" % supportFilesPath)
    checkpath("%s/Databases" % supportFilesPath)
    checkpath(logFilesPath)
    checkpath(frameworkSupportFilesPath)

    # Set the bundle path
    __bundlePath = _bundlePath.rstrip('/')

    # Add the bundle path to the system path, including any libraries
    if os.path.isdir("%s/Contents" % __bundlePath):
        sys.path.append("%s/Contents" % __bundlePath)
        if os.path.isdir("%s/Contents/Libraries" % __bundlePath):
            sys.path.append("%s/Contents/Libraries" % __bundlePath)
    else:
        print "Couldn't find bundle directory"
        return None

    # Open the Info.plist file
    f = open("%s/Contents/Info.plist" % __bundlePath, "r")
    infoplist = XML.ElementFromString(f.read())
    f.close()
    if infoplist is None:
        print "Couldn't load Info.plist file from plug-in"
        return

    # Get the plug-in identifier
    Identifier = infoplist.xpath(
        '//key[text()="CFBundleIdentifier"]//following-sibling::string/text()'
    )[0]
    if Identifier is None:
        print "Invalid Info.plist file in plug-in"
        return None

    # Set up the log file
    __logFilePath = "%s/%s.log" % (logFilesPath, Identifier)
    if os.path.exists(__logFilePath):
        if os.path.exists("%s.old" % __logFilePath):
            os.remove("%s.old" % __logFilePath)
        os.rename(__logFilePath, "%s.old" % __logFilePath)

    # Now we can start logging
    PMS.Log("(Framework) Bundle verification complete", False)

    # Check whether debugging is enabled
    try:
        _debug = infoplist.xpath(
            '//key[text()="PlexPluginDebug"]//following-sibling::string/text()'
        )[0]
        if _debug == "1":
            Debug = True
            PMS.Log("(Framework) Debugging is enabled")
    except:
        pass

    # Log the system encoding (set during bootstrap)
    PMS.Log("(Framework) Default encoding is " + sys.getdefaultencoding())

    # Set up framework paths
    Prefs.__prefsPath = "%s/Preferences/%s.xml" % (supportFilesPath,
                                                   Identifier)
    Data.__dataPath = "%s/Data/%s" % (supportFilesPath, Identifier)
    Data.__dataItemPath = "%s/DataItems" % Data.__dataPath
    if not os.path.isdir(Data.__dataItemPath):
        FirstRun = True
        os.makedirs(Data.__dataItemPath)
    Resource.__resourcePath = "%s/Contents/Resources" % __bundlePath
    Helper.__helperPath = "%s/Contents/Helpers" % __bundlePath
    Resource.__sharedResourcePath = "%s/Plug-ins/Framework.bundle/Contents/Resources/Versions/1/Resources" % pmsPath
    Database.__databasePath = "%s/Databases/%s.db" % (supportFilesPath,
                                                      Identifier)
    os.chdir(Data.__dataItemPath)
    Locale.SetDefaultLocale()
    PMS.Log("(Framework) Configured framework modules")

    # Attempt to import the plug-in module - if debugging is enabled, don't catch exceptions
    if Debug:
        import Code as _plugin
        PMS.Log("(Framework) Imported plug-in module")
    else:
        try:
            import Code as _plugin
            PMS.Log("(Framework) Imported plug-in module")
        except ImportError:
            PMS.Log("(Framework) Couldn't import plug-in from bundle")
            __exit()
            return

    # Load the list of trusted plug-ins
    _trusted = []
    try:
        _trustedJSON = Resource.LoadShared("trust.json")
        if _trustedJSON:
            _trusted = JSON.ObjectFromString(_trustedJSON)
    except:
        pass

    # Populate the permission lists
    __setupPermissionLists()

    # Register the plug-in with the framework
    __pluginModule = _plugin

    # Check the imported module to make sure nothing untoward is happening!
    if Identifier in _trusted:
        PMS.Log("(Framework) Plug-in is trusted, skipping module check")
    else:
        __scanModules()
        _allowed = []
        for n in PMS.__dict__:
            if n[0] != "_":
                if type(PMS.__dict__[n]).__name__ == "module":
                    _allowed.append(n)
        for n in __modWhitelist:
            _allowed.append(n)
        __checkModule(_plugin, _allowed)
        PMS.Log("(Framework) Checked module imports")

    # Initialize the framework modules
    Dict.__load()
    if not FirstRun:
        __checkFrameworkCompatibility()
    Prefs.__load()
    HTTP.__loadCookieJar()
    HTTP.__loadCache()
    PMS.Log("(Framework) Initialized framework modules")

    # Call the plug-in's Start method
    PMS.Log("(Framework) Attempting to start the plug-in...")
    __call(__pluginModule.Start)
    PMS.Log("(Framework) Plug-in started", False)

    # Start timers
    __startCacheManager(firstRun=FirstRun)

    PMS.Log("(Framework) Entering run loop")
    # Enter a run loop to handle requests
    while True:
        try:
            # Read the input
            path = raw_input()
            path = path.lstrip("GET ").strip()
            LastPrefix = None

            # Read headers
            headers = {}
            stop = False
            while stop == False:
                line = raw_input()
                if len(line) == 1:
                    stop = True
                else:
                    split = string.split(line.strip(), ":", maxsplit=1)
                    if len(split) == 2:
                        headers[split[0].strip()] = split[1].strip()

            # Set the locale
            if headers.has_key("X-Plex-Language"):
                loc = headers["X-Plex-Language"].lower()
                Locale.__loadLocale(loc)

            # Set the version
            if headers.has_key("X-Plex-Version"):
                Client.__setVersion(headers["X-Plex-Version"])

            # Extract arguments
            kwargs = {}
            mpath = path
            if path.find("?") >= 0:
                parts = path.split("?")
                mpath = parts[0]
                args = parts[1].split("&")
                for arg in args:
                    kwarg = arg.split("=")
                    if len(kwarg) == 2:
                        name = urllib.unquote(kwarg[0])
                        value = urllib.unquote(kwarg[1])
                        kwargs[name] = value
            if mpath[-1] == "/":
                mpath = mpath[:-1]

            # Split the path into components and decode.
            pathNouns = path.split('/')
            pathNouns = [urllib.unquote(p) for p in pathNouns]

            # If no input was given, return an error
            if len(pathNouns) <= 1:
                __return("%s\r\n\r\n" % PMS.Error['BadRequest'])

            # Otherwise, attempt to handle the request
            else:
                result = None
                pathNouns.pop(0)
                count = len(pathNouns)
                if pathNouns[-1] == "":
                    pathNouns.pop(len(pathNouns) - 1)
                PMS.Log("(Framework) Handling request :  %s" % path, False)

                # Check for a management request
                if pathNouns[0] == ":":
                    result = __handlePMSRequest(pathNouns, path, **kwargs)

                else:
                    handler = None
                    isPrefixHandler = False

                    # See if there's a prefix handler available
                    for key in __prefixHandlers:
                        if mpath.count(key, 0, len(key)) == 1:
                            LastPrefix = key
                    if mpath in __prefixHandlers:
                        handler = __prefixHandlers[mpath]["handler"]
                        isPrefixHandler = True

                    else:
                        # Check each request handler to see if it handles the current prefix
                        popped = False
                        for key in __requestHandlers:
                            if handler is None:
                                if path.count(key, 0, len(key)) == 1:
                                    # Remove the prefix from the path
                                    keyNounCount = len(key.split('/')) - 1
                                    for i in range(keyNounCount):
                                        pathNouns.pop(0)
                                    count = count - keyNounCount
                                    # Find the request handler
                                    handler = __requestHandlers[key]["handler"]
                                    LastPrefix = key
                                    popped = True

                        # If no path request handler was found, make sure we still pop the prefix so internal requests work
                        for key in __prefixHandlers:
                            if popped == False:
                                if mpath.count(key, 0, len(key)) == 1:
                                    keyNounCount = len(key.split('/')) - 1
                                    for i in range(keyNounCount):
                                        pathNouns.pop(0)
                                    popped = True

                    # Check whether we should handle the request internally
                    handled = False
                    if count > 0:
                        if pathNouns[0] == ":":
                            handled = True
                            result = __handleInternalRequest(
                                pathNouns, path, **kwargs)

                    # Check if the App Store has flagged the plug-in as broken
                    if os.path.exists(
                            os.path.join(frameworkSupportFilesPath,
                                         "%s.broken" % Identifier)):
                        #TODO: Localise this bit, use message from the App Store if available
                        handled = True
                        result = PMS.Objects.MessageContainer(
                            "Please try again later",
                            "This plug-in is currently unavailable")
                        PMS.Log("(Framework) Plug-in is flagged as broken")

                    # If the request hasn't been handled, and we have a valid request handler, call it
                    else:
                        if not handled and handler is not None:
                            if isPrefixHandler:
                                result = handler(**kwargs)
                            else:
                                result = handler(pathNouns, path, **kwargs)

                # If the request wasn't handled, return an error
                if result == None:
                    PMS.Log("(Framework) Request not handled by plug-in",
                            False)
                    response = "%s\r\n\r\n" % PMS.Error['NotFound']

                # If the plugin returned an error, return it to PMS
                elif result in PMS.Error.values():
                    PMS.Log(
                        "(Framework) Plug-in returned an error :  %s" % result,
                        False)
                    response = "%s\r\n" % result

                # Otherwise, check if a valid object was returned, and return the result
                elif __objectManager.ObjectHasBase(result, Objects.Object):
                    PMS.Log("(Framework) Response OK")
                    resultStr = result.Content()
                    resultStatus = result.Status()
                    resultHeaders = result.Headers()
                    if resultStr is not None:
                        resultLen = len(resultStr)
                        if resultLen > 0:
                            resultHeaders += "Content-Length: %i\r\n" % resultLen
                        resultStr = "\r\n%s" % resultStr
                    else:
                        resultStr = ""
                    response = str("%s\r\n%s" %
                                   (resultStatus, resultHeaders)) + str(
                                       resultStr) + str("\r\n")

                __return(response)

        # If a KeyboardInterrupt (SIGINT) is raised, stop the plugin
        except KeyboardInterrupt:
            # Save data & exit
            __saveData()
            __exit()

        except EOFError:
            # Save data & exit
            __saveData()
            __exit()

        # If another exception is raised, deal with the problem
        except:
            __except()
            __return("%s\r\n\r\n" % PMS.Error['InternalError'])

        # Make sure the plugin's data is saved
        finally:
            __saveData()
Example #21
0
import HTTP
import os
import sys


#set the iptables
os.system('sudo iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP')

# for TEST
#url = "http://david.choffnes.com/classes/cs4700sp16/2MB.log"
#url = "http://david.choffnes.com/"
#url = "http://david.choffnes.com/classes/cs4700sp16/10MB.log"

if len(sys.argv)<2:
	print "You need input target url"
	sys.exit(0)
url = sys.argv[1]
Request = HTTP.HTTPRequest(url)
Request.sendGetRequest()

Example #22
0
def Geolocation():
    return HTTP.Request("http://james.plexapp.com/apps/geolocate.php",
                        cacheTime=86400)


####################################################################################################
Example #23
0
def GetElement(url, cacheTime=0):
    data = HTTP.Get(url, cacheTime)
    return ElementSoup.parse(data)
Example #24
0
def __run(_bundlePath):
  #
  # Initializes the plugin framework, verifies the plugin & extracts information, then enters a
  # run loop for handling requests. 
  #
  global BundlePath
  global ResourcesPath
  global Identifier
  global DataPath
  global Debug
  
  global __pluginModule
  global __prefs
  global __prefsPath
  global __databasePath
  global __logFilePath
  global __requestHandlers
  
  if sys.platform == "win32":
    if 'PLEXLOCALAPPDATA' in os.environ:
      key = 'PLEXLOCALAPPDATA'
    else:
      key = 'LOCALAPPDATA'
    supportFilesPath = os.path.join(os.environ[key], "Plex Media Server", "Plug-in Support")
    logFilesPath = os.path.join(os.environ[key], "Plex Media Server", "Logs", "PMS Plugin Logs")
  else:
    supportFilesPath = os.environ["HOME"] + "/Library/Application Support/Plex Media Server/Plug-in Support"
    logFilesPath = os.environ["HOME"] + "/Library/Logs/Plex Media Server/PMS Plugin Logs/"
  
  def checkpath(path):
    try:
      if not os.path.exists(path): os.makedirs(path)
    except:
      pass
  
  checkpath(supportFilesPath + "/Preferences")
  checkpath(supportFilesPath + "/Databases")
  checkpath(logFilesPath)
  
  # Set the bundle path variable
  BundlePath = _bundlePath.rstrip('/')
  ResourcesPath = BundlePath + "/Contents/Resources"
  
  # Add the bundle path to the system path
  if os.path.isdir(BundlePath + "/Contents"):
    sys.path.append(BundlePath + "/Contents")
    if os.path.isdir(BundlePath + "/Contents/Libraries"):
      sys.path.append(BundlePath + "/Contents/Libraries")
  else:
    print "Couldn't find bundle directory"
    return None
  
  # Open the Info.plist file
  infoplist = XML.ElementFromFile((BundlePath + "/Contents/Info.plist"))
  if infoplist is None:
    print "Couldn't load Info.plist file from plugin"
    return

  # Get the plugin identifier
  Identifier = infoplist.xpath('//key[text()="CFBundleIdentifier"]//following-sibling::string/text()')[0]
  if Identifier is None:
    print "Invalid Info.plist file in plugin"
    return None
    
  # Set up the log file
  __logFilePath = logFilesPath + Identifier + ".log"
  if os.path.exists(__logFilePath):
    if os.path.exists(__logFilePath + ".old"):
      os.remove(__logFilePath + ".old")
    os.rename(__logFilePath, __logFilePath + ".old")
  
  # Show a big warning message - Framework v0 is deprecated!!
  Log.Add("(Framework) Deprecated version\n\nNOTICE: This version of the Plex Media Framework is deprecated and is no longer supported.\nPlease migrate your code to a newer version. More information can be found at http://dev.plexapp.com/\n")
  
  Log.Add("(Framework) Plugin initialized", False)
  
  # Check whether debugging is enabled
  try:
    _debug = infoplist.xpath('//key[text()="PlexPluginDebug"]//following-sibling::string/text()')[0]
    if _debug == "1":
      Debug = True
      Log.Add("(Framework) Debugging is enabled")
  except: pass
  
  # Create the data path if it doesn't already exist
  DataPath = supportFilesPath + "/Data/" + Identifier
  if not os.path.isdir(DataPath):
    os.makedirs(DataPath)
  
  # Change directory to the data path
  os.chdir(DataPath)
  
  # If a preference file exists, load it
  __prefsPath = supportFilesPath + "/Preferences/" + Identifier + ".xml"
  defaultsPath = BundlePath + "/Contents/Defaults.xml"
  if os.path.exists(__prefsPath):
    __prefs = XML.ElementFromFile(__prefsPath)
    Log.Add("(Framework) Loaded user preferences")

  # Otherwise, try to apply the defaults file
  elif os.path.exists(defaultsPath):
    __prefs = XML.ElementFromFile(defaultsPath)
    Log.Add("(Framework) Loaded default preferences")
    
  # If no preferences were loaded, create an empty preferences element
  else:
    __prefs = XML.Element("PluginPreferences")
  
  # Load the plugin's dictionary file
  LoadDict()
  
  # Load the plugin's localization strings
  Locale.__loadDefaults()
  # TODO: Retrieve locale info from PMS for overriding default dict strings 
  # Locale.__loadLocale(loc)
  
  # Set the database file path
  __databasePath = supportFilesPath + "/Databases/" + Identifier + ".db"
  
  # Initialize the plugin's CookieJar
  HTTP.__loadCookieJar()
  Log.Add("(Framework) Loaded cookie jar")
  
  if HTTP_TIMEOUT_VAR_NAME in os.environ:
    HTTP.SetTimeout(float(os.environ[HTTP_TIMEOUT_VAR_NAME]))
  else:
    HTTP.SetTimeout(HTTP_DEFAULT_TIMEOUT)
  
  # Attempt to import the plugin module - if debugging is enabled, don't catch exceptions
  if Debug:
    import Code as _plugin
    Log.Add("(Framework) Imported plugin module")
  else:
    try:
      import Code as _plugin
      Log.Add("(Framework) Imported plugin module")
    except ImportError:
      Log.Add("(Framework) Couldn't import plugin from bundle")
      return
  
  __pluginModule = _plugin
  
  # Call the plugin's Start method
  Log.Add("(Framework) Attempting to start the plugin...")
  __call(_plugin.Start)
  Log.Add("(Framework) Plugin started", False)


  Log.Add("(Framework) Entering run loop")
  # Enter a run loop to handle requests
  while True:
    try:
      # Read the input
      path = raw_input()
      
      # Strip GET from the start of the path
      path = path.lstrip("GET ").strip()
      
      # Split the path into components and decode.
      pathNouns = path.replace('?query=', '/').split('/')
      pathNouns = [urllib.unquote(p) for p in pathNouns]
      
      # If no input was given, return an error
      if len(pathNouns) <= 1:
        sys.stdout.write("%s\r\n\r\n" % PMS.Error['BadRequest'])
        sys.stdout.flush()
        
      # Otherwise, attempt to handle the request
      else:
        Response['Content-Type'] = 'application/xml'
        Response['Status'] = '200 OK'
        Response["Headers"] = ""
        result = None
        pathNouns.pop(0)
        count = len(pathNouns)
        if pathNouns[count-1] == "":
          count = count - 1
          pathNouns.pop(len(pathNouns)-1)
          
        Log.Add("(Framework) Handling request :  " + path, False)
        
        # Check for a management request
        if pathNouns[0] == ":":
          result = __handlePMSRequest(pathNouns, count)

        else:  
          # Check each request handler to see if it handles the current prefix
          handler = None
          for key in __requestHandlers:
            if handler is None:
              if path.count(key, 0, len(key)) == 1:
                # Remove the prefix from the path
                keyNounCount = len(key.split('/')) - 1
                for i in range(keyNounCount):
                  pathNouns.pop(0)
                count = count - keyNounCount
                # Find the request handler
                handler = __requestHandlers[key]["handler"]
                
          # Check whether we should handle the request internally
          handled = False
          if count > 0:
            if pathNouns[0] == ":":
              handled = True
              result = __handleInternalRequest(pathNouns, count)
    
          # If the request hasn't been handled, and we have a valid request handler, call it
          if not handled and handler is not None:
            result = handler(pathNouns, count)
        
        # If the request wasn't handled, return an error
        if result == None:
          Log.Add("(Framework) Request not handled by plugin", False)
          response = "%s\r\n\r\n" % PMS.Error['NotFound']
          
        # If the plugin returned an error, return it to PMS
        elif result in PMS.Error.values():
          Log.Add("(Framework) Plug-in returned an error :  %s" % result, False)
          response = result + "\r\n"
          
        # Otherwise, return the result
        else:
          Log.Add("(Framework) Response OK")
          response = "%s\r\nContent-Type: %s\r\nContent-Length: %i\r\n%s\r\n%s\r\n" % \
            (Response["Status"], str(Response['Content-Type']), len(result), Response["Headers"], result)

        sys.stdout.write(response)
        sys.stdout.flush()
    
    # If a KeyboardInterrupt (SIGINT) is raised, stop the plugin
    except KeyboardInterrupt:
      # Commit any changes to the database and close it
      if DB.__db is not None:
        DB.Commit()
        DB.__db.close()
      # Save the dictionary
      SaveDict()
      # Exit
      sys.exit()
    
    except EOFError:
      # Commit any changes to the database and close it
      if DB.__db is not None:
        DB.Commit()
        DB.__db.close()
      Log.Add("(Framework) Plugin stopped")
      sys.exit()
          
    # If another exception is raised, deal with the problem
    except:
      # If in debug mode, print the traceback, otherwise report an internal error
      if Debug:
        Log.Add("(Framework) An exception happened:\n%s" % traceback.format_exc())
      else:
        Log.Add("(Framework) An internal error occurred", False)
      sys.stdout.write("%s\r\n\r\n" % PMS.Error['InternalError'])
      sys.stdout.flush()
    
    # Make sure the plugin's dictionary is saved
    finally:
      SaveDict()
Example #25
0
def Geolocation():
  return HTTP.Get("http://api.hostip.info/country.php")

####################################################################################################
def analyses_toute_les_trames(Liste_trames, fichier):
    """list [ tuple(list[octects], bool, int ,int) ] -> None
    Analyse toutes les trames recuperees """

    length = len(Liste_trames)  # nombre de trames recuperees

    # Parcours de toutes les trames recuperees
    for i in range(length):

        Trame, ok, ligne_erreur, taille = Liste_trames[i]

        print("\n********************* Analyse de la trame", i + 1,
              "******************** \n")

        # if la trame est errone en raison des offsets invalides
        if Trame == []:
            print(
                " \nLa trame", i,
                "est erronee car l'offset n'est pas dans l'ordre croissant\n")
            fichier.write("*\n!\n")
            continue

        # if il manque des octets dans la trame
        if not ok:
            print("      \nLa trame", i, "a rencontré une erreur à la ligne ",
                  ligne_erreur, " de sa trame\n")
            fichier.write("*\n!\n")
            continue

        print(taille, " octets de donnees")

        # donne des noms differents a chaque trame et leurs composantes
        str_num = str(i)
        str_Ethernet = "Ethernet_" + str_num
        str_IP = "IP_" + str_num
        str_TCP = "TCP_" + str_num
        str_HTTP = "HTTP_" + str_num

        # Entete Ethernet
        dest_mac = Trame[:6]  # Destination (Adresse MAC)
        src_mac = Trame[6:12]  # Soruce (Adresse MAC)
        type_ethernet = Trame[12:14]  # Type Ethernet

        str_Ethernet = Ethernet(dest_mac, src_mac, type_ethernet)
        str_Ethernet.affichage()
        str_Ethernet.affichage_bouton(fichier)

        # If la trame n'est pas IPv4 then aller à la trame suivante
        if not est_IPv4(Trame):
            print("Notre analyseur ne prend pas en compte le protocole 0x" +
                  ''.join(Trame[12:14]))
            fichier.write("14 " + str(taille))
            continue

        # Entete IPv4
        version = Trame[14][0]  # Version
        header_length_ip = Trame[14][1]  # Header Length
        dsf = Trame[15]  # Differentiated Sevices Field
        total_length = Trame[16:18]  # Total Length
        id = Trame[18:20]  # Identification
        flags_ip = Trame[20]  # Flags
        offset = Trame[20:22]  # Framgment offset
        ttl = Trame[22]  # Time to live
        protocol = Trame[23]  # Protocol
        checksum_ip = Trame[24:26]  # Header Checksum
        src_ip = Trame[26:30]  # Source IP
        dest_ip = Trame[30:34]  # Destination IP
        options_ip = []  # Options IP

        str_IP = IP(version, header_length_ip, dsf, total_length, id, flags_ip,
                    offset, ttl, protocol, checksum_ip, src_ip, dest_ip,
                    options_ip)

        taille_options_IP = str_IP.taille_options_IP(
        )  # Recupere la taille des options IP si elles existent
        fin_IP = 34 + taille_options_IP  # Recupere le dernier octect de l'entete IP
        str_IP.set_options(Trame[34:fin_IP])  # Affectation des options de IP
        str_IP.verification_checksum(Trame[14:fin_IP])  # Check le checksum

        str_IP.affichage()
        str_IP.affichage_bouton(fichier)

        # if la trame recuperee n'est pas de la taille de totale_length
        if not nombre_octects_exacts(taille,
                                     int(''.join(str_IP.total_length), 16)):
            print("La trame contient ", taille,
                  "octets alors qu'elle devrait en contenir",
                  int(''.join(str_IP.total_length), 16) + 14)
            continue

        # if la trame n'est pas TCP then aller à la trame suivante
        if not est_TCP(Trame):
            print("\nNotre analyseur ne prend pas en compte le protocole 0x" +
                  ''.join(Trame[23]))
            fichier.write(str(fin_IP) + " " + str(taille) + "\n")
            continue

        # Entete TCP
        debut_TCP = fin_IP  # premier octer du segment TCP
        src_port = Trame[debut_TCP:debut_TCP + 2]  # Source Port
        dest_port = Trame[debut_TCP + 2:debut_TCP + 4]  # Destination Port
        sequence = Trame[debut_TCP + 4:debut_TCP + 8]  # Sequence number
        acknowledgment = Trame[debut_TCP + 8:debut_TCP +
                               12]  # acknowledgment number
        header_length_tcp = Trame[debut_TCP + 12]  # Header Length
        flags_tcp = Trame[debut_TCP + 12:debut_TCP + 14]  # Flags
        window = Trame[debut_TCP + 14:debut_TCP + 16]  # Window
        checksum_tcp = Trame[debut_TCP + 16:debut_TCP + 18]  # Checksum
        urgent_pointer = Trame[debut_TCP + 18:debut_TCP + 20]  # Urgent pointer
        options_tcp = []  # Options
        TCP_length = str(
            hex(
                int(''.join(str_IP.total_length), 16) - 20 -
                taille_options_IP)[2:].zfill(4))  # Taille du segment TCP

        str_TCP = TCP(src_port, dest_port, sequence, acknowledgment,
                      header_length_tcp, flags_tcp, window, checksum_tcp,
                      urgent_pointer, options_tcp)
        str_TCP.set_options(Trame[debut_TCP + 20:debut_TCP + 20 +
                                  str_TCP.taille_options_TCP()])
        str_TCP.verification_checksum(src_ip + dest_ip + ["00"] + [protocol] +
                                      [TCP_length[0:2]] + [TCP_length[2:4]] +
                                      Trame[debut_TCP:])  # Check le checksum
        str_TCP.affichage()
        str_TCP.affichage_bouton(fichier, debut_TCP)

        taille_options_TCP = str_TCP.taille_options_TCP(
        )  # Recupère la taille des options TCP si elles existent
        fin_TCP = debut_TCP + 20 + taille_options_TCP  # Recupere le dernier octect de l'entete IP

        # if pas de data then aller a la trame suivante
        if fin_TCP == taille:
            continue

        # if la trame n'est pas HTTP then aller à la trame suivante
        if not est_HTTP(Trame, src_port, dest_port):
            print(
                "\nNotre analyseur ne peut pas lire le contenu de ce segment (pas HTTP)"
            )
            continue

        debut_HTTP = fin_TCP  # Récupère le premier octet du segment HTTP
        str_HTTP = HTTP(Trame[debut_HTTP:])
        str_HTTP.affichage()
        str_HTTP.affichage_bouton(fichier, debut_HTTP)

    return None
Example #27
0
 def addStartURL(self, url):
     if (self.__checklink(url)):
         print(_("Invalid link argument: {0}").format(url))
         sys.exit(0)
     if (self.__inzone(url) == 0):
         self.tobrowse.append(HTTP.HTTPResource(url))
Example #28
0
#encoding:utf-8
import requests

import HTTP

base_url = 'https://www.toutiao.com'
data = 'data'
pc_feed_focus = 'pc_feed_focus'
url = 'https://www.toutiao.com/api/pc/focus/'  #今日头条主页
news = HTTP.http2(url, data, pc_feed_focus)
for new in news:
    display_url = new['display_url']
    sec_url = base_url + display_url
    sec_data = requests.get(sec_url).text
    print sec_data
Example #29
0
            if page_encoding and page_encoding.upper() != resp_encoding:
                # Mismatch ! Convert the response text to the encoding detected by BeautifulSoup
                resp.setEncoding(page_encoding)
            data = resp.getPage()
        else:
            # Can't find an encoding... beware of non-html content
            data = resp.getRawPage()

        # Manage redirections
        if "location" in info:
            redir = self.correctlink(info["location"], current, currentdir,
                                     proto, None)
            if redir is not None:
                if (self.__inzone(redir) == 0):
                    self.link_encoding[redir] = self.link_encoding[url]
                    redir = HTTP.HTTPResource(redir)
                    # Is the document already visited of forbidden ?
                    if (redir in self.browsed) or (redir in self.tobrowse) or \
                            self.isExcluded(redir):
                        pass
                    else:
                        # No -> Will browse it soon
                        self.tobrowse.append(redir)

        htmlSource = data
        if page_encoding:
            bs = BeautifulSoup.BeautifulSoup(htmlSource)
            # Look for a base tag with an href attribute
            if bs.head:
                baseTags = bs.head.findAll("base")
                for base in baseTags:
def __run(_bundlePath):
  #
  # Initializes the plugin framework, verifies the plugin & extracts information, then enters a
  # run loop for handling requests. 
  #
  global BundlePath
  global ResourcesPath
  global Identifier
  global DataPath
  global Debug
  
  global __pluginModule
  global __prefs
  global __prefsPath
  global __databasePath
  global __logFilePath
  global __requestHandlers
  
  if sys.platform == "win32":
    if 'PLEXLOCALAPPDATA' in os.environ:
      key = 'PLEXLOCALAPPDATA'
    else:
      key = 'LOCALAPPDATA'
    supportFilesPath = os.path.join(os.environ[key], "Plex Media Server", "Plug-in Support")
    logFilesPath = os.path.join(os.environ[key], "Plex Media Server", "Logs", "PMS Plugin Logs")
  else:
    supportFilesPath = os.environ["HOME"] + "/Library/Application Support/Plex Media Server/Plug-in Support"
    logFilesPath = os.environ["HOME"] + "/Library/Logs/PMS Plugin Logs/"
  
  def checkpath(path):
    try:
      if not os.path.exists(path): os.makedirs(path)
    except:
      pass
  
  checkpath(supportFilesPath + "/Preferences")
  checkpath(supportFilesPath + "/Databases")
  checkpath(logFilesPath)
  
  # Set the bundle path variable
  BundlePath = _bundlePath.rstrip('/')
  ResourcesPath = BundlePath + "/Contents/Resources"
  
  # Add the bundle path to the system path
  if os.path.isdir(BundlePath + "/Contents"):
    sys.path.append(BundlePath + "/Contents")
    if os.path.isdir(BundlePath + "/Contents/Libraries"):
      sys.path.append(BundlePath + "/Contents/Libraries")
  else:
    print "Couldn't find bundle directory"
    return None
  
  # Open the Info.plist file
  infoplist = XML.ElementFromFile((BundlePath + "/Contents/Info.plist"))
  if infoplist is None:
    print "Couldn't load Info.plist file from plugin"
    return

  # Get the plugin identifier
  Identifier = infoplist.xpath('//key[text()="CFBundleIdentifier"]//following-sibling::string/text()')[0]
  if Identifier is None:
    print "Invalid Info.plist file in plugin"
    return None
    
  # Set up the log file
  __logFilePath = logFilesPath + Identifier + ".log"
  if os.path.exists(__logFilePath):
    if os.path.exists(__logFilePath + ".old"):
      os.remove(__logFilePath + ".old")
    os.rename(__logFilePath, __logFilePath + ".old")
  
  # Show a big warning message - Framework v0 is deprecated!!
  Log.Add("(Framework) Deprecated version\n\nNOTICE: This version of the Plex Media Framework is deprecated and is no longer supported.\nPlease migrate your code to a newer version. More information can be found at http://dev.plexapp.com/\n")
  
  Log.Add("(Framework) Plugin initialized", False)
  
  # Check whether debugging is enabled
  try:
    _debug = infoplist.xpath('//key[text()="PlexPluginDebug"]//following-sibling::string/text()')[0]
    if _debug == "1":
      Debug = True
      Log.Add("(Framework) Debugging is enabled")
  except: pass
  
  # Create the data path if it doesn't already exist
  DataPath = supportFilesPath + "/Data/" + Identifier
  if not os.path.isdir(DataPath):
    os.makedirs(DataPath)
  
  # Change directory to the data path
  os.chdir(DataPath)
  
  # If a preference file exists, load it
  __prefsPath = supportFilesPath + "/Preferences/" + Identifier + ".xml"
  defaultsPath = BundlePath + "/Contents/Defaults.xml"
  if os.path.exists(__prefsPath):
    __prefs = XML.ElementFromFile(__prefsPath)
    Log.Add("(Framework) Loaded user preferences")

  # Otherwise, try to apply the defaults file
  elif os.path.exists(defaultsPath):
    __prefs = XML.ElementFromFile(defaultsPath)
    Log.Add("(Framework) Loaded default preferences")
    
  # If no preferences were loaded, create an empty preferences element
  else:
    __prefs = XML.Element("PluginPreferences")
  
  # Load the plugin's dictionary file
  LoadDict()
  
  # Load the plugin's localization strings
  Locale.__loadDefaults()
  # TODO: Retrieve locale info from PMS for overriding default dict strings 
  # Locale.__loadLocale(loc)
  
  # Set the database file path
  __databasePath = supportFilesPath + "/Databases/" + Identifier + ".db"
  
  # Initialize the plugin's CookieJar
  HTTP.__loadCookieJar()
  Log.Add("(Framework) Loaded cookie jar")
  
  if HTTP_TIMEOUT_VAR_NAME in os.environ:
    HTTP.SetTimeout(float(os.environ[HTTP_TIMEOUT_VAR_NAME]))
  else:
    HTTP.SetTimeout(HTTP_DEFAULT_TIMEOUT)
  
  # Attempt to import the plugin module - if debugging is enabled, don't catch exceptions
  if Debug:
    import Code as _plugin
    Log.Add("(Framework) Imported plugin module")
  else:
    try:
      import Code as _plugin
      Log.Add("(Framework) Imported plugin module")
    except ImportError:
      Log.Add("(Framework) Couldn't import plugin from bundle")
      return
  
  __pluginModule = _plugin
  
  # Call the plugin's Start method
  Log.Add("(Framework) Attempting to start the plugin...")
  __call(_plugin.Start)
  Log.Add("(Framework) Plugin started", False)


  Log.Add("(Framework) Entering run loop")
  # Enter a run loop to handle requests
  while True:
    try:
      # Read the input
      path = raw_input()
      
      # Strip GET from the start of the path
      path = path.lstrip("GET ").strip()
      
      # Split the path into components and decode.
      pathNouns = path.replace('?query=', '/').split('/')
      pathNouns = [urllib.unquote(p) for p in pathNouns]
      
      # If no input was given, return an error
      if len(pathNouns) <= 1:
        sys.stdout.write("%s\r\n\r\n" % PMS.Error['BadRequest'])
        sys.stdout.flush()
        
      # Otherwise, attempt to handle the request
      else:
        Response['Content-Type'] = 'application/xml'
        Response['Status'] = '200 OK'
        Response["Headers"] = ""
        result = None
        pathNouns.pop(0)
        count = len(pathNouns)
        if pathNouns[count-1] == "":
          count = count - 1
          pathNouns.pop(len(pathNouns)-1)
          
        Log.Add("(Framework) Handling request :  " + path, False)
        
        # Check for a management request
        if pathNouns[0] == ":":
          result = __handlePMSRequest(pathNouns, count)

        else:  
          # Check each request handler to see if it handles the current prefix
          handler = None
          for key in __requestHandlers:
            if handler is None:
              if path.count(key, 0, len(key)) == 1:
                # Remove the prefix from the path
                keyNounCount = len(key.split('/')) - 1
                for i in range(keyNounCount):
                  pathNouns.pop(0)
                count = count - keyNounCount
                # Find the request handler
                handler = __requestHandlers[key]["handler"]
                
          # Check whether we should handle the request internally
          handled = False
          if count > 0:
            if pathNouns[0] == ":":
              handled = True
              result = __handleInternalRequest(pathNouns, count)
    
          # If the request hasn't been handled, and we have a valid request handler, call it
          if not handled and handler is not None:
            result = handler(pathNouns, count)
        
        # If the request wasn't handled, return an error
        if result == None:
          Log.Add("(Framework) Request not handled by plugin", False)
          response = "%s\r\n\r\n" % PMS.Error['NotFound']
          
        # If the plugin returned an error, return it to PMS
        elif result in PMS.Error.values():
          Log.Add("(Framework) Plug-in returned an error :  %s" % result, False)
          response = result + "\r\n"
          
        # Otherwise, return the result
        else:
          Log.Add("(Framework) Response OK")
          response = "%s\r\nContent-Type: %s\r\nContent-Length: %i\r\n%s\r\n%s\r\n" % \
            (Response["Status"], str(Response['Content-Type']), len(result), Response["Headers"], result)

        sys.stdout.write(response)
        sys.stdout.flush()
    
    # If a KeyboardInterrupt (SIGINT) is raised, stop the plugin
    except KeyboardInterrupt:
      # Commit any changes to the database and close it
      if DB.__db is not None:
        DB.Commit()
        DB.__db.close()
      # Save the dictionary
      SaveDict()
      # Exit
      sys.exit()
    
    except EOFError:
      # Commit any changes to the database and close it
      if DB.__db is not None:
        DB.Commit()
        DB.__db.close()
      Log.Add("(Framework) Plugin stopped")
      sys.exit()
          
    # If another exception is raised, deal with the problem
    except:
      # If in debug mode, print the traceback, otherwise report an internal error
      if Debug:
        Log.Add("(Framework) An exception happened:\n%s" % traceback.format_exc())
      else:
        Log.Add("(Framework) An internal error occurred", False)
      sys.stdout.write("%s\r\n\r\n" % PMS.Error['InternalError'])
      sys.stdout.flush()
    
    # Make sure the plugin's dictionary is saved
    finally:
      SaveDict()