Пример #1
0
def AddUPnPMapping( internal_client, internal_port, external_port, protocol, description, duration = 3600 ):
    
    cmd = '"' + upnpc_path + '" -e "' + description + '" -a ' + internal_client + ' ' + str( internal_port ) + ' ' + str( external_port ) + ' ' + protocol + ' ' + str( duration )
    
    p = subprocess.Popen( shlex.split( cmd ), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, startupinfo = HydrusData.GetHideTerminalSubprocessStartupInfo() )
    
    HydrusData.WaitForProcessToFinish( p, 30 )
    
    ( output, error ) = p.communicate()
    
    if 'x.x.x.x:' + str( external_port ) + ' TCP is redirected to internal ' + internal_client + ':' + str( internal_port ) in output:
        
        raise HydrusExceptions.FirewallException( 'The UPnP mapping of ' + internal_client + ':' + internal_port + '->external:' + external_port + ' already exists as a port forward. If this UPnP mapping is automatic, please disable it.' )
        
    
    if output is not None and 'failed with code' in output:
        
        if 'UnknownError' in output:
            
            raise HydrusExceptions.FirewallException( 'Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode( output ) )
            
        else:
            
            raise Exception( 'Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode( output ) )
            
        
    
    if error is not None and len( error ) > 0:
        
        raise Exception( 'Problem while trying to add UPnP mapping:' + os.linesep * 2 + HydrusData.ToUnicode( error ) )
Пример #2
0
 def _GetResponse( self, method_string, path_and_query, request_headers, body, attempt_number = 1 ):
     
     try:
         
         self._connection.request( method_string, path_and_query, headers = request_headers, body = body )
         
         return self._connection.getresponse()
         
     except ( httplib.CannotSendRequest, httplib.BadStatusLine ):
         
         # for some reason, we can't send a request on the current connection, so let's make a new one and try again!
         
         time.sleep( 1 )
         
         if attempt_number <= 3:
             
             self._RefreshConnection()
             
             return self._GetResponse( method_string, path_and_query, request_headers, body, attempt_number = attempt_number + 1 )
             
         else:
             
             raise
             
         
     except socket.error as e:
         
         if e.errno == errno.WSAEACCES:
             
             text = 'The hydrus client did not have permission to make a connection to ' + HydrusData.ToUnicode( self._host )
             
             if self._port is not None:
                 
                 text += ' on port ' + HydrusData.ToUnicode( self._port )
                 
             
             text += '. This is usually due to a firewall stopping it.'
             
             raise HydrusExceptions.FirewallException( text )
             
         elif e.errno == errno.WSAECONNRESET:
             
             time.sleep( 5 )
             
             if attempt_number <= 3:
                 
                 self._RefreshConnection()
                 
                 return self._GetResponse( method_string, path_and_query, request_headers, body, attempt_number = attempt_number + 1 )
                 
             else:
                 
                 text = 'The hydrus client\'s connection to ' + HydrusData.ToUnicode( self._host ) + ' kept on being reset by the remote host, so the attempt was abandoned.'
                 
                 raise HydrusExceptions.NetworkException( text )
                 
             
         else:
             
             raise
Пример #3
0
    def _GetInitialResponse(self,
                            method,
                            path_and_query,
                            request_headers,
                            body,
                            attempt_number=1):

        if method == HC.GET: method_string = 'GET'
        elif method == HC.POST: method_string = 'POST'

        try:

            self._connection.request(method_string,
                                     path_and_query,
                                     headers=request_headers,
                                     body=body)

            return (self._connection.getresponse(), attempt_number)

        except (httplib.CannotSendRequest, httplib.BadStatusLine):

            # for some reason, we can't send a request on the current connection, so let's make a new one and try again!

            time.sleep(1)

            if attempt_number <= 3:

                self._RefreshConnection()

                return self._GetInitialResponse(method,
                                                path_and_query,
                                                request_headers,
                                                body,
                                                attempt_number=attempt_number +
                                                1)

            else:

                raise

        except socket.error as e:

            if HC.PLATFORM_WINDOWS:

                access_errors = [errno.EACCES, errno.WSAEACCES]
                connection_reset_errors = [
                    errno.ECONNRESET, errno.WSAECONNRESET
                ]

            else:

                access_errors = [errno.EACCES]
                connection_reset_errors = [errno.ECONNRESET]

            if e.errno in access_errors:

                text = 'The hydrus client did not have permission to make a connection to ' + HydrusData.ToUnicode(
                    self._host)

                if self._port is not None:

                    text += ' on port ' + HydrusData.ToUnicode(self._port)

                text += '. This is usually due to a firewall stopping it.'

                raise HydrusExceptions.FirewallException(text)

            elif e.errno in connection_reset_errors:

                time.sleep(5)

                if attempt_number <= 3:

                    self._RefreshConnection()

                    return self._GetInitialResponse(
                        method,
                        path_and_query,
                        request_headers,
                        body,
                        attempt_number=attempt_number + 1)

                else:

                    text = 'The hydrus client\'s connection to ' + HydrusData.ToUnicode(
                        self._host
                    ) + ' kept on being reset by the remote host, so the attempt was abandoned.'

                    raise HydrusExceptions.NetworkException(text)

            else:

                raise

        except ssl.SSLEOFError:

            time.sleep(5)

            if attempt_number <= 3:

                self._RefreshConnection()

                return self._GetInitialResponse(method_string,
                                                path_and_query,
                                                request_headers,
                                                body,
                                                attempt_number=attempt_number +
                                                1)

            else:

                text = 'The hydrus client\'s ssl connection to ' + HydrusData.ToUnicode(
                    self._host
                ) + ' kept terminating abruptly, so the attempt was abandoned.'

                raise HydrusExceptions.NetworkException(text)