home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_SOCKET.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  4.3 KB  |  147 lines

  1. # Not tested:
  2. #       socket.fromfd()
  3. #       sktobj.getsockopt()
  4. #       sktobj.recvfrom()
  5. #       sktobj.sendto()
  6. #       sktobj.setblocking()
  7. #       sktobj.setsockopt()
  8. #       sktobj.shutdown()
  9.  
  10.  
  11. from test_support import verbose, TestFailed
  12. import socket
  13. import os
  14. import time
  15. import string
  16.  
  17. def missing_ok(str):
  18.     try:
  19.         getattr(socket, str)
  20.     except AttributeError:
  21.         pass
  22.  
  23. try: raise socket.error
  24. except socket.error: print "socket.error"
  25.  
  26. socket.AF_INET
  27.  
  28. socket.SOCK_STREAM
  29. socket.SOCK_DGRAM
  30. socket.SOCK_RAW
  31. socket.SOCK_RDM
  32. socket.SOCK_SEQPACKET
  33.  
  34. for optional in ("AF_UNIX",
  35.  
  36.                  "SO_DEBUG", "SO_ACCEPTCONN", "SO_REUSEADDR", "SO_KEEPALIVE",
  37.                  "SO_DONTROUTE", "SO_BROADCAST", "SO_USELOOPBACK", "SO_LINGER",
  38.                  "SO_OOBINLINE", "SO_REUSEPORT", "SO_SNDBUF", "SO_RCVBUF",
  39.                  "SO_SNDLOWAT", "SO_RCVLOWAT", "SO_SNDTIMEO", "SO_RCVTIMEO",
  40.                  "SO_ERROR", "SO_TYPE", "SOMAXCONN",
  41.  
  42.                  "MSG_OOB", "MSG_PEEK", "MSG_DONTROUTE", "MSG_EOR",
  43.                  "MSG_TRUNC", "MSG_CTRUNC", "MSG_WAITALL", "MSG_BTAG",
  44.                  "MSG_ETAG",
  45.  
  46.                  "SOL_SOCKET",
  47.  
  48.                  "IPPROTO_IP", "IPPROTO_ICMP", "IPPROTO_IGMP",
  49.                  "IPPROTO_GGP", "IPPROTO_TCP", "IPPROTO_EGP",
  50.                  "IPPROTO_PUP", "IPPROTO_UDP", "IPPROTO_IDP",
  51.                  "IPPROTO_HELLO", "IPPROTO_ND", "IPPROTO_TP",
  52.                  "IPPROTO_XTP", "IPPROTO_EON", "IPPROTO_BIP",
  53.                  "IPPROTO_RAW", "IPPROTO_MAX",
  54.  
  55.                  "IPPORT_RESERVED", "IPPORT_USERRESERVED",
  56.  
  57.                  "INADDR_ANY", "INADDR_BROADCAST", "INADDR_LOOPBACK",
  58.                  "INADDR_UNSPEC_GROUP", "INADDR_ALLHOSTS_GROUP",
  59.                  "INADDR_MAX_LOCAL_GROUP", "INADDR_NONE",
  60.  
  61.                  "IP_OPTIONS", "IP_HDRINCL", "IP_TOS", "IP_TTL",
  62.                  "IP_RECVOPTS", "IP_RECVRETOPTS", "IP_RECVDSTADDR",
  63.                  "IP_RETOPTS", "IP_MULTICAST_IF", "IP_MULTICAST_TTL",
  64.                  "IP_MULTICAST_LOOP", "IP_ADD_MEMBERSHIP",
  65.                  "IP_DROP_MEMBERSHIP",
  66.                  ):
  67.     missing_ok(optional)
  68.  
  69. socktype = socket.SocketType
  70. hostname = socket.gethostname()
  71. ip = socket.gethostbyname(hostname)
  72. hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
  73. all_host_names = [hname] + aliases
  74.  
  75. if verbose:
  76.     print hostname
  77.     print ip
  78.     print hname, aliases, ipaddrs
  79.     print all_host_names
  80.  
  81. for name in all_host_names:
  82.     if string.find(name, '.'):
  83.         break
  84. else:
  85.     print 'FQDN not found'
  86.  
  87. print socket.getservbyname('telnet', 'tcp')
  88. try:
  89.     socket.getservbyname('telnet', 'udp')
  90. except socket.error:
  91.     pass
  92.  
  93.  
  94. canfork = hasattr(os, 'fork')
  95. try:
  96.     PORT = 50007
  97.     if not canfork or os.fork():
  98.         # parent is server
  99.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  100.         s.bind(("127.0.0.1", PORT))
  101.         s.listen(1)
  102.         if verbose:
  103.             print 'parent accepting'
  104.         if canfork:
  105.             conn, addr = s.accept()
  106.             if verbose:
  107.                 print 'connected by', addr
  108.             # couple of interesting tests while we've got a live socket
  109.             f = conn.fileno()
  110.             if verbose:
  111.                 print 'fileno:', f
  112.             p = conn.getpeername()
  113.             if verbose:
  114.                 print 'peer:', p
  115.             n = conn.getsockname()
  116.             if verbose:
  117.                 print 'sockname:', n
  118.             f = conn.makefile()
  119.             if verbose:
  120.                 print 'file obj:', f
  121.             while 1:
  122.                 data = conn.recv(1024)
  123.                 if not data:
  124.                     break
  125.                 if verbose:
  126.                     print 'received:', data
  127.                 conn.send(data)
  128.             conn.close()
  129.     else:
  130.         try:
  131.             # child is client
  132.             time.sleep(5)
  133.             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  134.             if verbose:
  135.                 print 'child connecting'
  136.             s.connect(("127.0.0.1", PORT))
  137.             msg = 'socket test'
  138.             s.send(msg)
  139.             data = s.recv(1024)
  140.             if msg <> data:
  141.                 print 'parent/client mismatch'
  142.             s.close()
  143.         finally:
  144.             os._exit(1)
  145. except socket.error, msg:
  146.     raise TestFailed, msg
  147.