Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:7.2 socketUp:7.2 socketNext:7.2.2 SSL Objects

 
7.2.1 Socket Objects

Socket objects have the following methods. Except formakefile() these correspond to Unix system callsapplicable to sockets.

accept()
Accept a connection.The socket must be bound to an address and listening for connections.The return value is a pair(conn,address)whereconn is anew socket object usable to send andreceive data on the connection, andaddress is the address boundto the socket on the other end of the connection.

bind(address)
Bind the socket toaddress. The socket must not already be bound.(The format ofaddress depends on the address family -- seeabove.)Note:This method has historically accepted a pairof parameters forAF_INET addresses instead of only atuple. This was never intentional and is no longer be available inPython 2.0.

close()
Close the socket. All future operations on the socket object will fail.The remote end will receive no more data (after queued data is flushed).Sockets are automatically closed when they are garbage-collected.

connect(address)
Connect to a remote socket ataddress.(The format ofaddress depends on the address family -- seeabove.)Note:This method has historically accepted a pairof parameters forAF_INET addresses instead of only atuple. This was never intentional and is no longer available inPython 2.0 and later.

connect_ex(address)
Likeconnect(address), but return an error indicatorinstead of raising an exception for errors returned by the C-levelconnect() call (other problems, such as ``host not found,''can still raise exceptions). The error indicator is0 if theoperation succeeded, otherwise the value of theerrnovariable. This is useful to support, for example, asynchronous connects.Note:This method has historically accepted a pair ofparameters forAF_INET addresses instead of only a tuple.This was never intentional and is no longer be available in Python2.0 and later.

fileno()
Return the socket's file descriptor (a small integer). This is usefulwithselect.select().

getpeername()
Return the remote address to which the socket is connected. This isuseful to find out the port number of a remote IPv4/v6 socket, for instance.(The format of the address returned depends on the address family --see above.) On some systems this function is not supported.

getsockname()
Return the socket's own address. This is useful to find out the portnumber of an IPv4/v6 socket, for instance.(The format of the address returned depends on the address family --see above.)

getsockopt(level, optname[, buflen])
Return the value of the given socket option (see the Unix man pagegetsockopt(2)). The needed symbolic constants(SO_* etc.) are defined in this module. Ifbuflenis absent, an integer option is assumed and its integer valueis returned by the function. Ifbuflen is present, it specifiesthe maximum length of the buffer used to receive the option in, andthis buffer is returned as a string. It is up to the caller to decodethe contents of the buffer (see the optional built-in modulestruct for a way to decode C structures encoded as strings).

listen(backlog)
Listen for connections made to the socket. Thebacklog argumentspecifies the maximum number of queued connections and should be atleast 1; the maximum value is system-dependent (usually 5).

makefile([mode[, bufsize]])
Return afile object associated with the socket. (File objectsare described in2.2.8, ``File Objects.'')The file object references adup()ped version of thesocket file descriptor, so the file object and socket object may beclosed or garbage-collected independently. The optionalmodeandbufsize arguments are interpreted the same way as by thebuilt-infile() function; see ``Built-in Functions''(section2.1) for more information.

recv(bufsize[, flags])
Receive data from the socket. The return value is a string representingthe data received. The maximum amount of data to be receivedat once is specified bybufsize. See the Unix manual pagerecv(2) for the meaning of the optional argumentflags; it defaults to zero.

recvfrom(bufsize[, flags])
Receive data from the socket. The return value is a pair(string,address) wherestring is a stringrepresenting the data received andaddress is the address of thesocket sending the data. The optionalflags argument has thesame meaning as forrecv() above.(The format ofaddress depends on the address family -- see above.)

send(string[, flags])
Send data to the socket. The socket must be connected to a remotesocket. The optionalflags argument has the same meaning as forrecv() above. Returns the number of bytes sent.Applications are responsible for checking that all data has been sent;if only some of the data was transmitted, the application needs toattempt delivery of the remaining data.

sendall(string[, flags])
Send data to the socket. The socket must be connected to a remotesocket. The optionalflags argument has the same meaning as forrecv() above. Unlikesend(), this method continuesto send data fromstring until either all data has been sent oran error occurs.None is returned on success. On error, anexception is raised, and there is no way to determine how much data,if any, was successfully sent.

sendto(string[, flags], address)
Send data to the socket. The socket should not be connected to aremote socket, since the destination socket is specified byaddress. The optionalflags argument has the samemeaning as forrecv() above. Return the number of bytes sent.(The format ofaddress depends on the address family -- see above.)

setblocking(flag)
Set blocking or non-blocking mode of the socket: ifflag is 0,the socket is set to non-blocking, else to blocking mode. Initiallyall sockets are in blocking mode. In non-blocking mode, if arecv() call doesn't find any data, or if asend() call can't immediately dispose of the data, aerror exception is raised; in blocking mode, the callsblock until they can proceed.

setsockopt(level, optname, value)
Set the value of the given socket option (see the Unix manual pagesetsockopt(2)). The needed symbolic constants are defined inthesocket module (SO_* etc.). The value can be aninteger or a string representing a buffer. In the latter case it isup to the caller to ensure that the string contains the proper bits(see the optional built-in modulestruct for a way to encode Cstructures as strings).

shutdown(how)
Shut down one or both halves of the connection. Ifhow is0, further receives are disallowed. Ifhow is1,further sends are disallowed. Ifhow is2, further sendsand receives are disallowed.

Note that there are no methodsread() orwrite();userecv() andsend() withoutflags argumentinstead.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:7.2 socketUp:7.2 socketNext:7.2.2 SSL Objects
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp