summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>1997-01-03 20:03:32 (GMT)
committerBarry Warsaw <barry@python.org>1997-01-03 20:03:32 (GMT)
commitcf3d4b51236d23a6652875937e90ad9cad3fc937 (patch)
treebc11b8cb4152f7deee556181bc300ea3e67d3cdf /Lib
parenteaa6e1102fd1ee97829e1d5b4b62663c1f6d7a0a (diff)
downloadcpython-cf3d4b51236d23a6652875937e90ad9cad3fc937.zip
cpython-cf3d4b51236d23a6652875937e90ad9cad3fc937.tar.gz
cpython-cf3d4b51236d23a6652875937e90ad9cad3fc937.tar.bz2
Test of the socket module. The following functions and methods are
not currently tested (or even touched): # socket.fromfd() # sktobj.getsockopt() # sktobj.recvfrom() # sktobj.sendto() # sktobj.setblocking() # sktobj.setsockopt() # sktobj.shutdown()
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_socket.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
new file mode 100644
index 0000000..6b45383
--- /dev/null
+++ b/Lib/test/test_socket.py
@@ -0,0 +1,131 @@
+# Not tested:
+# socket.fromfd()
+# sktobj.getsockopt()
+# sktobj.recvfrom()
+# sktobj.sendto()
+# sktobj.setblocking()
+# sktobj.setsockopt()
+# sktobj.shutdown()
+
+
+from test_support import verbose
+import socket
+import os
+import time
+
+def missing_ok(str):
+ try:
+ getattr(socket, str)
+ except AttributeError:
+ pass
+
+print socket.error
+
+print socket.AF_INET
+
+print socket.SOCK_STREAM
+print socket.SOCK_DGRAM
+print socket.SOCK_RAW
+print socket.SOCK_RDM
+print socket.SOCK_SEQPACKET
+
+for optional in ("AF_UNIX",
+
+ "SO_DEBUG", "SO_ACCEPTCONN", "SO_REUSEADDR", "SO_KEEPALIVE",
+ "SO_DONTROUTE", "SO_BROADCAST", "SO_USELOOPBACK", "SO_LINGER",
+ "SO_OOBINLINE", "SO_REUSEPORT", "SO_SNDBUF", "SO_RCVBUF",
+ "SO_SNDLOWAT", "SO_RCVLOWAT", "SO_SNDTIMEO", "SO_RCVTIMEO",
+ "SO_ERROR", "SO_TYPE", "SOMAXCONN",
+
+ "MSG_OOB", "MSG_PEEK", "MSG_DONTROUTE", "MSG_EOR",
+ "MSG_TRUNC", "MSG_CTRUNC", "MSG_WAITALL", "MSG_BTAG",
+ "MSG_ETAG",
+
+ "SOL_SOCKET",
+
+ "IPPROTO_IP", "IPPROTO_ICMP", "IPPROTO_IGMP",
+ "IPPROTO_GGP", "IPPROTO_TCP", "IPPROTO_EGP",
+ "IPPROTO_PUP", "IPPROTO_UDP", "IPPROTO_IDP",
+ "IPPROTO_HELLO", "IPPROTO_ND", "IPPROTO_TP",
+ "IPPROTO_XTP", "IPPROTO_EON", "IPPROTO_BIP",
+ "IPPROTO_RAW", "IPPROTO_MAX",
+
+ "IPPORT_RESERVED", "IPPORT_USERRESERVED",
+
+ "INADDR_ANY", "INADDR_BROADCAST", "INADDR_LOOPBACK",
+ "INADDR_UNSPEC_GROUP", "INADDR_ALLHOSTS_GROUP",
+ "INADDR_MAX_LOCAL_GROUP", "INADDR_NONE",
+
+ "IP_OPTIONS", "IP_HDRINCL", "IP_TOS", "IP_TTL",
+ "IP_RECVOPTS", "IP_RECVRETOPTS", "IP_RECVDSTADDR",
+ "IP_RETOPTS", "IP_MULTICAST_IF", "IP_MULTICAST_TTL",
+ "IP_MULTICAST_LOOP", "IP_ADD_MEMBERSHIP",
+ "IP_DROP_MEMBERSHIP",
+ ):
+ missing_ok(optional)
+
+hostname = socket.gethostname()
+ip = socket.gethostbyname(hostname)
+hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
+
+if verbose:
+ print hostname
+ print ip
+ print hname, aliases, ipaddrs
+
+if hostname <> hname:
+ print 'hostname mismatch'
+
+print socket.getservbyname('telnet', 'tcp')
+try:
+ socket.getservbyname('telnet', 'udp')
+except socket.error:
+ pass
+
+
+try:
+ PORT = 50007
+ if os.fork():
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.bind(hostname, PORT)
+ s.listen(1)
+ if verbose:
+ print 'parent accepting'
+ conn, addr = s.accept()
+ if verbose:
+ print 'connected by', addr
+ # couple of interesting tests while we've got a live socket
+ f = conn.fileno()
+ if verbose:
+ print 'fileno:', f
+ p = conn.getpeername()
+ if verbose:
+ print 'peer:', p
+ n = conn.getsockname()
+ if verbose:
+ print 'sockname:', n
+ f = conn.makefile()
+ if verbose:
+ print 'file obj:', f
+ while 1:
+ data = conn.recv(1024)
+ if not data:
+ break
+ if verbose:
+ print 'received:', data
+ conn.send(data)
+ conn.close()
+ else:
+ time.sleep(1)
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ if verbose:
+ print 'child connecting'
+ s.connect(hostname, PORT)
+ msg = 'socket test'
+ s.send(msg)
+ data = s.recv(1024)
+ if msg <> data:
+ print 'parent/client mismatch'
+ s.close()
+except socket.error:
+ pass