Exemplo n.º 1
0
    def testSamePort(self):
        path = os.path.join(os.path.dirname(__file__), '../logo-base.png')
        share = self.get_share(path)
        self.success_called_back = False
        def success_callback(share):
            self.success_called_back = True
            print 'port forwarding success is called'
        share.success_observers.append(success_callback)

        self.start_sharing(share)

        i = 0
        while i < 200 and not self.success_called_back:
            i += 1
            sleep(0.01)
        print "escaped at ",i
        self.assertTrue(self.success_called_back)
        port = share.server_port

        # apparently, the request is not needed, but hey, lets keep it.
        url = 'http://www.openport.be/debug/linkSessionsToPids?key=batterycupspoon'
        req = urllib2.Request(url)
        response = urllib2.urlopen(req).read()
        self.assertEqual('done', response.strip())

        dict = openport.request_port(
            key=get_or_create_public_key(),
            restart_session_token=share.server_session_token,
            request_server_port=port
        )
        response = PortForwardResponse(dict)
        self.assertEqual(port, response.remote_port)

        dict = openport.request_port(
            key=get_or_create_public_key(),
            restart_session_token='not the same token',
            request_server_port=port
        )
        response = PortForwardResponse(dict)
        self.assertNotEqual(port, response.remote_port)
Exemplo n.º 2
0
def request_open_port(local_port, restart_session_token = '', request_server_port=-1, error_callback=None):

    public_key = get_or_create_public_key()

    logger.debug("requesting port forward - remote port: %s, restart session token: %s" % (request_server_port, restart_session_token))
    dict = request_port( public_key, restart_session_token=restart_session_token, request_server_port=request_server_port )

    if 'error' in dict:
        if error_callback:
            error_callback('An error has occured:\n%s' %(dict['error']))
        sys.exit(8)
    logger.debug(dict)

    response = PortForwardResponse(dict)

    if request_server_port != -1 and request_server_port != response.remote_port:
        logger.error( 'Did not get requested server port (%s), but got %s' % (request_server_port, response.remote_port))

    return response