home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / s / socket < prev    next >
Encoding:
Text File  |  1996-11-14  |  7.1 KB  |  143 lines

  1. <TITLE>socket -- Python library reference</TITLE>
  2. Prev: <A HREF="../s/signal" TYPE="Prev">signal</A>  
  3. Up: <A HREF="../o/optional_operating_system_services" TYPE="Up">Optional Operating System Services</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H1>7.2. Built-in Module <CODE>socket</CODE></H1>
  6. This module provides access to the BSD <I>socket</I> interface.
  7. It is available on UNIX systems that support this interface.
  8. <P>
  9. For an introduction to socket programming (in C), see the following
  10. papers: <I>An Introductory 4.3BSD Interprocess Communication
  11. Tutorial</I>, by Stuart Sechrest and <I>An Advanced 4.3BSD Interprocess
  12. Communication Tutorial</I>, by Samuel J.  Leffler et al, both in the
  13. UNIX Programmer's Manual, Supplementary Documents 1 (sections PS1:7
  14. and PS1:8).  The UNIX manual pages for the various socket-related
  15. system calls are also a valuable source of information on the details of
  16. socket semantics.
  17. <P>
  18. The Python interface is a straightforward transliteration of the
  19. UNIX system call and library interface for sockets to Python's
  20. object-oriented style: the <CODE>socket()</CODE> function returns a
  21. <DFN>socket object</DFN> whose methods implement the various socket system
  22. calls.  Parameter types are somewhat higer-level than in the C
  23. interface: as with <CODE>read()</CODE> and <CODE>write()</CODE> operations on Python
  24. files, buffer allocation on receive operations is automatic, and
  25. buffer length is implicit on send operations.
  26. <P>
  27. Socket addresses are represented as a single string for the
  28. <CODE>AF_UNIX</CODE> address family and as a pair
  29. <CODE>(<VAR>host</VAR>, <VAR>port</VAR>)</CODE> for the <CODE>AF_INET</CODE> address family,
  30. where <VAR>host</VAR> is a string representing
  31. either a hostname in Internet domain notation like
  32. <CODE>'daring.cwi.nl'</CODE> or an IP address like <CODE>'100.50.200.5'</CODE>,
  33. and <VAR>port</VAR> is an integral port number.  Other address families are
  34. currently not supported.  The address format required by a particular
  35. socket object is automatically selected based on the address family
  36. specified when the socket object was created.
  37. <P>
  38. All errors raise exceptions.  The normal exceptions for invalid
  39. argument types and out-of-memory conditions can be raised; errors
  40. related to socket or address semantics raise the error <CODE>socket.error</CODE>.
  41. <P>
  42. Non-blocking mode is supported through the <CODE>setblocking()</CODE>
  43. method.
  44. <P>
  45. The module <CODE>socket</CODE> exports the following constants and functions:
  46. <P>
  47. <DL><DT><B>error</B> -- exception of module socket<DD>
  48. This exception is raised for socket- or address-related errors.
  49. The accompanying value is either a string telling what went wrong or a
  50. pair <CODE>(<VAR>errno</VAR>, <VAR>string</VAR>)</CODE>
  51. representing an error returned by a system
  52. call, similar to the value accompanying <CODE>posix.error</CODE>.
  53. </DL>
  54. <DL><DT><B>AF_UNIX</B> -- data of module socket<DD>
  55. </DL>
  56. <DL><DT><B>AF_INET</B> -- data of module socket<DD>
  57. These constants represent the address (and protocol) families,
  58. used for the first argument to <CODE>socket()</CODE>.  If the <CODE>AF_UNIX</CODE>
  59. constant is not defined then this protocol is unsupported.
  60. </DL>
  61. <DL><DT><B>SOCK_STREAM</B> -- data of module socket<DD>
  62. </DL>
  63. <DL><DT><B>SOCK_DGRAM</B> -- data of module socket<DD>
  64. </DL>
  65. <DL><DT><B>SOCK_RAW</B> -- data of module socket<DD>
  66. </DL>
  67. <DL><DT><B>SOCK_RDM</B> -- data of module socket<DD>
  68. </DL>
  69. <DL><DT><B>SOCK_SEQPACKET</B> -- data of module socket<DD>
  70. These constants represent the socket types,
  71. used for the second argument to <CODE>socket()</CODE>.
  72. (Only <CODE>SOCK_STREAM</CODE> and
  73. <CODE>SOCK_DGRAM</CODE> appear to be generally useful.)
  74. </DL>
  75. <DL><DT><B>SO_*</B> -- data of module socket<DD>
  76. </DL>
  77. <DL><DT><B>SOMAXCONN</B> -- data of module socket<DD>
  78. </DL>
  79. <DL><DT><B>MSG_*</B> -- data of module socket<DD>
  80. </DL>
  81. <DL><DT><B>SOL_*</B> -- data of module socket<DD>
  82. </DL>
  83. <DL><DT><B>IPPROTO_*</B> -- data of module socket<DD>
  84. </DL>
  85. <DL><DT><B>IPPORT_*</B> -- data of module socket<DD>
  86. </DL>
  87. <DL><DT><B>INADDR_*</B> -- data of module socket<DD>
  88. </DL>
  89. <DL><DT><B>IP_*</B> -- data of module socket<DD>
  90. Many constants of these forms, documented in the UNIX documentation on
  91. sockets and/or the IP protocol, are also defined in the socket module.
  92. They are generally used in arguments to the <CODE>setsockopt</CODE> and
  93. <CODE>getsockopt</CODE> methods of socket objects.  In most cases, only
  94. those symbols that are defined in the UNIX header files are defined;
  95. for a few symbols, default values are provided.
  96. </DL>
  97. <DL><DT><B>gethostbyname</B> (<VAR>hostname</VAR>) -- function of module socket<DD>
  98. Translate a host name to IP address format.  The IP address is
  99. returned as a string, e.g.,  <CODE>'100.50.200.5'</CODE>.  If the host name
  100. is an IP address itself it is returned unchanged.
  101. </DL>
  102. <DL><DT><B>gethostname</B> () -- function of module socket<DD>
  103. Return a string containing the hostname of the machine where 
  104. the Python interpreter is currently executing.  If you want to know the
  105. current machine's IP address, use
  106. <CODE>socket.gethostbyname(socket.gethostname())</CODE>.
  107. </DL>
  108. <DL><DT><B>gethostbyaddr</B> (<VAR>ip_address</VAR>) -- function of module socket<DD>
  109. Return a triple <CODE>(hostname, aliaslist, ipaddrlist)</CODE> where
  110. <CODE>hostname</CODE> is the primary host name responding to the given
  111. <VAR>ip_address</VAR>, <CODE>aliaslist</CODE> is a (possibly empty) list of
  112. alternative host names for the same address, and <CODE>ipaddrlist</CODE> is
  113. a list of IP addresses for the same interface on the same
  114. host (most likely containing only a single address).
  115. </DL>
  116. <DL><DT><B>getservbyname</B> (<VAR>servicename</VAR>, <VAR>protocolname</VAR>) -- function of module socket<DD>
  117. Translate an Internet service name and protocol name to a port number
  118. for that service.  The protocol name should be <CODE>'tcp'</CODE> or
  119. <CODE>'udp'</CODE>.
  120. </DL>
  121. <DL><DT><B>socket</B> (<VAR>family</VAR>, <VAR>type</VAR>[, <VAR>proto</VAR>]) -- function of module socket<DD>
  122. Create a new socket using the given address family, socket type and
  123. protocol number.  The address family should be <CODE>AF_INET</CODE> or
  124. <CODE>AF_UNIX</CODE>.  The socket type should be <CODE>SOCK_STREAM</CODE>,
  125. <CODE>SOCK_DGRAM</CODE> or perhaps one of the other `<SAMP>SOCK_</SAMP>' constants.
  126. The protocol number is usually zero and may be omitted in that case.
  127. </DL>
  128. <DL><DT><B>fromfd</B> (<VAR>fd</VAR>, <VAR>family</VAR>, <VAR>type</VAR>[, <VAR>proto</VAR>]) -- function of module socket<DD>
  129. Build a socket object from an existing file descriptor (an integer as
  130. returned by a file object's <CODE>fileno</CODE> method).  Address family,
  131. socket type and protocol number are as for the <CODE>socket</CODE> function
  132. above.  The file descriptor should refer to a socket, but this is not
  133. checked --- subsequent operations on the object may fail if the file
  134. descriptor is invalid.  This function is rarely needed, but can be
  135. used to get or set socket options on a socket passed to a program as
  136. standard input or output (e.g. a server started by the UNIX inet
  137. daemon).
  138. </DL>
  139. <H2>Menu</H2><DL COMPACT>
  140. <DT><A HREF="../s/socket_objects" TYPE=Menu>Socket Objects</A>
  141. <DD><DT><A HREF="../s/socket_example" TYPE=Menu>Socket Example</A>
  142. <DD></DL>
  143.