def test_connect (self): port = test_support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(socket.error) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
def test_create_connection (self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = test_support.find_unused_port() with self.assertRaises(socket.error) as cm: socket.create_connection((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
def testRudeShutdown(self): listener_ready = threading.Event() listener_gone = threading.Event() port = test_support.find_unused_port() # `listener` runs in a thread. It opens a socket listening on # PORT, and sits in an accept() until the main thread connects. # Then it rudely closes the socket, and sets Event `listener_gone` # to let the main thread know the socket is gone. def listener(): s = socket.socket() s.bind((HOST, port)) s.listen(5) listener_ready.set() s.accept() s = None # reclaim the socket object, which also closes it listener_gone.set() def connector(): listener_ready.wait() s = socket.socket() s.connect((HOST, port)) listener_gone.wait() try: ssl_sock = ssl.wrap_socket(s) except IOError: pass else: raise test_support.TestFailed("connecting to closed SSL socket should have failed") t = threading.Thread(target=listener) t.start() connector() t.join()
def test_connect(self): port = test_support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(socket.error) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = test_support.find_unused_port() with self.assertRaises(socket.error) as cm: socket.create_connection((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
def test_main(verbose=False): if skip_expected: raise test_support.TestSkipped("No SSL support") global CERTFILE, SVN_PYTHON_ORG_ROOT_CERT CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") SVN_PYTHON_ORG_ROOT_CERT = os.path.join(os.path.dirname(__file__) or os.curdir, "https_svn_python_org_root.pem") if not os.path.exists(CERTFILE) or not os.path.exists(SVN_PYTHON_ORG_ROOT_CERT): raise test_support.TestFailed("Can't read certificate files!") TESTPORT = test_support.find_unused_port() if not TESTPORT: raise test_support.TestFailed("Can't find open port to test servers on!") tests = [BasicTests] if test_support.is_resource_enabled("network"): tests.append(NetworkedTests) if _have_threads: thread_info = test_support.threading_setup() if thread_info and test_support.is_resource_enabled("network"): tests.append(ThreadedTests) test_support.run_unittest(*tests) if _have_threads: test_support.threading_cleanup(*thread_info)
def test_main(verbose=False): if skip_expected: raise test_support.TestSkipped("No SSL support") global CERTFILE, SVN_PYTHON_ORG_ROOT_CERT CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") SVN_PYTHON_ORG_ROOT_CERT = os.path.join( os.path.dirname(__file__) or os.curdir, "https_svn_python_org_root.pem") if (not os.path.exists(CERTFILE) or not os.path.exists(SVN_PYTHON_ORG_ROOT_CERT)): raise test_support.TestFailed("Can't read certificate files!") TESTPORT = test_support.find_unused_port() if not TESTPORT: raise test_support.TestFailed("Can't find open port to test servers on!") tests = [BasicTests] if test_support.is_resource_enabled('network'): tests.append(NetworkedTests) if _have_threads: thread_info = test_support.threading_setup() if thread_info and test_support.is_resource_enabled('network'): tests.append(ThreadedTests) test_support.run_unittest(*tests) if _have_threads: test_support.threading_cleanup(*thread_info)
def __init__(self, certfile): self.flag = None self.active = False self.RootedHTTPRequestHandler.root = os.path.split(CERTFILE)[0] self.port = test_support.find_unused_port() self.server = self.HTTPSServer((HOST, self.port), self.RootedHTTPRequestHandler, certfile) threading.Thread.__init__(self) self.daemon = True
def __init__(self, certfile): self.flag = None self.active = False self.RootedHTTPRequestHandler.root = os.path.split(CERTFILE)[0] self.port = test_support.find_unused_port() self.server = self.HTTPSServer( (HOST, self.port), self.RootedHTTPRequestHandler, certfile) threading.Thread.__init__(self) self.daemon = True
def setUp(self): self.serv_evt = threading.Event() self.client_evt = threading.Event() self.port = test_support.find_unused_port() self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1)) serv_args = (self.serv, self.serv_evt, self.client_evt) threading.Thread(target=debugging_server, args=serv_args).start() # wait until server thread has assigned a port number self.serv_evt.wait() self.serv_evt.clear()
def setUp(self): # temporarily replace sys.stdout to capture DebuggingServer output self.old_stdout = sys.stdout self.output = StringIO.StringIO() sys.stdout = self.output self.serv_evt = threading.Event() self.client_evt = threading.Event() self.port = test_support.find_unused_port() self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1)) serv_args = (self.serv, self.serv_evt, self.client_evt) threading.Thread(target=debugging_server, args=serv_args).start() # wait until server thread has assigned a port number self.serv_evt.wait() self.serv_evt.clear()
def testRudeShutdown(self): listener_ready = threading.Event() listener_gone = threading.Event() port = test_support.find_unused_port() # `listener` runs in a thread. It opens a socket listening on # PORT, and sits in an accept() until the main thread connects. # Then it rudely closes the socket, and sets Event `listener_gone` # to let the main thread know the socket is gone. def listener(): s = socket.socket() s.bind((HOST, port)) s.listen(5) listener_ready.set() s.accept() s = None # reclaim the socket object, which also closes it listener_gone.set() def connector(): listener_ready.wait() s = socket.socket() s.connect((HOST, port)) listener_gone.wait() try: ssl_sock = ssl.wrap_socket(s) except IOError: pass else: raise test_support.TestFailed( 'connecting to closed SSL socket should have failed') t = threading.Thread(target=listener) t.start() connector() t.join()
def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.port = test_support.bind_port(self.serv) self.source_port = test_support.find_unused_port() self.serv.listen(5) self.conn = None
def clientSetUp(self): self.source_port = test_support.find_unused_port()
def testWithoutServer(self): port = test_support.find_unused_port() self.failUnlessRaises( socket.error, lambda: socket.create_connection((HOST, port)) )
def testWithoutServer(self): port = test_support.find_unused_port() self.failUnlessRaises(socket.error, lambda: socket.create_connection( (HOST, port)))