PyXR

c:\python24\lib \ test \ test_socket_ssl.py



0001 # Test just the SSL support in the socket module, in a moderately bogus way.
0002 
0003 from test import test_support
0004 import socket
0005 import time
0006 
0007 # Optionally test SSL support.  This requires the 'network' resource as given
0008 # on the regrtest command line.
0009 skip_expected = not (test_support.is_resource_enabled('network') and
0010                      hasattr(socket, "ssl"))
0011 
0012 def test_basic():
0013     test_support.requires('network')
0014 
0015     import urllib
0016 
0017     socket.RAND_status()
0018     try:
0019         socket.RAND_egd(1)
0020     except TypeError:
0021         pass
0022     else:
0023         print "didn't raise TypeError"
0024     socket.RAND_add("this is a random string", 75.0)
0025 
0026     f = urllib.urlopen('https://sf.net')
0027     buf = f.read()
0028     f.close()
0029 
0030 def test_rude_shutdown():
0031     try:
0032         import thread
0033     except ImportError:
0034         return
0035 
0036     # some random port to connect to
0037     PORT = 9934
0038     def listener():
0039         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
0040         s.bind(('', PORT))
0041         s.listen(5)
0042         s.accept()
0043         del s
0044         thread.exit()
0045 
0046     def connector():
0047         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
0048         s.connect(('localhost', PORT))
0049         try:
0050             ssl_sock = socket.ssl(s)
0051         except socket.sslerror:
0052             pass
0053         else:
0054             raise test_support.TestFailed, \
0055                         'connecting to closed SSL socket failed'
0056 
0057     thread.start_new_thread(listener, ())
0058     time.sleep(1)
0059     connector()
0060 
0061 def test_main():
0062     if not hasattr(socket, "ssl"):
0063         raise test_support.TestSkipped("socket module has no ssl support")
0064     test_rude_shutdown()
0065     test_basic()
0066 
0067 if __name__ == "__main__":
0068     test_main()
0069 

Generated by PyXR 0.9.4
SourceForge.net Logo