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

  1. <TITLE>Socket Objects -- Python library reference</TITLE>
  2. Next: <A HREF="../s/socket_example" TYPE="Next">Socket Example</A>  
  3. Prev: <A HREF="../s/socket" TYPE="Prev">socket</A>  
  4. Up: <A HREF="../s/socket" TYPE="Up">socket</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>7.2.1. Socket Objects</H2>
  7.  Socket objects have the following methods.  Except for
  8. <CODE>makefile()</CODE> these correspond to UNIX system calls applicable to
  9. sockets.
  10. <P>
  11. <DL><DT><B>accept</B> () -- Method on socket<DD>
  12. Accept a connection.
  13. The socket must be bound to an address and listening for connections.
  14. The return value is a pair <CODE>(<VAR>conn</VAR>, <VAR>address</VAR>)</CODE>
  15. where <VAR>conn</VAR> is a <I>new</I> socket object usable to send and
  16. receive data on the connection, and <VAR>address</VAR> is the address bound
  17. to the socket on the other end of the connection.
  18. </DL>
  19. <DL><DT><B>bind</B> (<VAR>address</VAR>) -- Method on socket<DD>
  20. Bind the socket to <VAR>address</VAR>.  The socket must not already be bound.
  21. (The format of <VAR>address</VAR> depends on the address family --- see above.)
  22. </DL>
  23. <DL><DT><B>close</B> () -- Method on socket<DD>
  24. Close the socket.  All future operations on the socket object will fail.
  25. The remote end will receive no more data (after queued data is flushed).
  26. Sockets are automatically closed when they are garbage-collected.
  27. </DL>
  28. <DL><DT><B>connect</B> (<VAR>address</VAR>) -- Method on socket<DD>
  29. Connect to a remote socket at <VAR>address</VAR>.
  30. (The format of <VAR>address</VAR> depends on the address family --- see above.)
  31. </DL>
  32. <DL><DT><B>fileno</B> () -- Method on socket<DD>
  33. Return the socket's file descriptor (a small integer).  This is useful
  34. with <CODE>select</CODE>.
  35. </DL>
  36. <DL><DT><B>getpeername</B> () -- Method on socket<DD>
  37. Return the remote address to which the socket is connected.  This is
  38. useful to find out the port number of a remote IP socket, for instance.
  39. (The format of the address returned depends on the address family ---
  40. see above.)  On some systems this function is not supported.
  41. </DL>
  42. <DL><DT><B>getsockname</B> () -- Method on socket<DD>
  43. Return the socket's own address.  This is useful to find out the port
  44. number of an IP socket, for instance.
  45. (The format of the address returned depends on the address family ---
  46. see above.)
  47. </DL>
  48. <DL><DT><B>getsockopt</B> (<VAR>level</VAR>, <VAR>optname</VAR>[, <VAR>buflen</VAR>]) -- Method on socket<DD>
  49. Return the value of the given socket option (see the UNIX man page
  50. <I>getsockopt</I>(2)).  The needed symbolic constants (<CODE>SO_*</CODE> etc.)
  51. are defined in this module.  If <VAR>buflen</VAR>
  52. is absent, an integer option is assumed and its integer value
  53. is returned by the function.  If <VAR>buflen</VAR> is present, it specifies
  54. the maximum length of the buffer used to receive the option in, and
  55. this buffer is returned as a string.  It is up to the caller to decode
  56. the contents of the buffer (see the optional built-in module
  57. <CODE>struct</CODE> for a way to decode C structures encoded as strings).
  58. </DL>
  59. <DL><DT><B>listen</B> (<VAR>backlog</VAR>) -- Method on socket<DD>
  60. Listen for connections made to the socket.  The <VAR>backlog</VAR> argument
  61. specifies the maximum number of queued connections and should be at
  62. least 1; the maximum value is system-dependent (usually 5).
  63. </DL>
  64. <DL><DT><B>makefile</B> ([<VAR>mode</VAR>[, <VAR>bufsize</VAR>]]) -- Method on socket<DD>
  65. Return a <DFN>file object</DFN> associated with the socket.  (File objects
  66. were described earlier under Built-in Types.)  The file object
  67. references a <CODE>dup()</CODE>ped version of the socket file descriptor, so
  68. the file object and socket object may be closed or garbage-collected
  69. independently.  The optional <VAR>mode</VAR> and <VAR>bufsize</VAR> arguments
  70. are interpreted the same way as by the built-in
  71. <CODE>open()</CODE> function.
  72. </DL>
  73. <DL><DT><B>recv</B> (<VAR>bufsize</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
  74. Receive data from the socket.  The return value is a string representing
  75. the data received.  The maximum amount of data to be received
  76. at once is specified by <VAR>bufsize</VAR>.  See the UNIX manual page
  77. for the meaning of the optional argument <VAR>flags</VAR>; it defaults to
  78. zero.
  79. </DL>
  80. <DL><DT><B>recvfrom</B> (<VAR>bufsize</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
  81. Receive data from the socket.  The return value is a pair
  82. <CODE>(<VAR>string</VAR>, <VAR>address</VAR>)</CODE> where <VAR>string</VAR> is a string
  83. representing the data received and <VAR>address</VAR> is the address of the
  84. socket sending the data.  The optional <VAR>flags</VAR> argument has the
  85. same meaning as for <CODE>recv()</CODE> above.
  86. (The format of <VAR>address</VAR> depends on the address family --- see above.)
  87. </DL>
  88. <DL><DT><B>send</B> (<VAR>string</VAR>[, <VAR>flags</VAR>]) -- Method on socket<DD>
  89. Send data to the socket.  The socket must be connected to a remote
  90. socket.  The optional <VAR>flags</VAR> argument has the same meaning as for
  91. <CODE>recv()</CODE> above.  Return the number of bytes sent.
  92. </DL>
  93. <DL><DT><B>sendto</B> (<VAR>string</VAR>[, <VAR>flags</VAR>], <VAR>address</VAR>) -- Method on socket<DD>
  94. Send data to the socket.  The socket should not be connected to a
  95. remote socket, since the destination socket is specified by
  96. <CODE>address</CODE>.  The optional <VAR>flags</VAR> argument has the same
  97. meaning as for <CODE>recv()</CODE> above.  Return the number of bytes sent.
  98. (The format of <VAR>address</VAR> depends on the address family --- see above.)
  99. </DL>
  100. <DL><DT><B>setblocking</B> (<VAR>flag</VAR>) -- Method on socket<DD>
  101. Set blocking or non-blocking mode of the socket: if <VAR>flag</VAR> is 0,
  102. the socket is set to non-blocking, else to blocking mode.  Initially
  103. all sockets are in blocking mode.  In non-blocking mode, if a
  104. <CODE>recv</CODE> call doesn't find any data, or if a <CODE>send</CODE> call can't
  105. immediately dispose of the data, a <CODE>socket.error</CODE> exception is
  106. raised; in blocking mode, the calls block until they can proceed.
  107. </DL>
  108. <DL><DT><B>setsockopt</B> (<VAR>level</VAR>, <VAR>optname</VAR>, <VAR>value</VAR>) -- Method on socket<DD>
  109. Set the value of the given socket option (see the UNIX man page
  110. <I>setsockopt</I>(2)).  The needed symbolic constants are defined in
  111. the <CODE>socket</CODE> module (<CODE>SO_*</CODE> etc.).  The value can be an
  112. integer or a string representing a buffer.  In the latter case it is
  113. up to the caller to ensure that the string contains the proper bits
  114. (see the optional built-in module
  115. <CODE>struct</CODE> for a way to encode C structures as strings).
  116. </DL>
  117. <DL><DT><B>shutdown</B> (<VAR>how</VAR>) -- Method on socket<DD>
  118. Shut down one or both halves of the connection.  If <VAR>how</VAR> is <CODE>0</CODE>,
  119. further receives are disallowed.  If <VAR>how</VAR> is <CODE>1</CODE>, further sends are
  120. disallowed.  If <VAR>how</VAR> is <CODE>2</CODE>, further sends and receives are
  121. disallowed.
  122. </DL>
  123. Note that there are no methods <CODE>read()</CODE> or <CODE>write()</CODE>; use
  124. <CODE>recv()</CODE> and <CODE>send()</CODE> without <VAR>flags</VAR> argument instead.
  125. <P>
  126.