python的socket跟java差不多,首先可以用socket根据url获取IP,比如用python的socket通过“www.baidu.com”来获取百度的IP
>>> import socket
>>> ip = socket.gethostbyname("www.baidu.com")
>>> print(ip)
183.232.231.173
或者是通过socket获取本机IP
>>> ip = socket.gethostbyname("localhost")
>>> print(ip)
127.0.0.1
python的socket还有其他很多东西
>>> help(socket)
Help on module socket:
NAME
socket
DESCRIPTION
This module provides socket operations and some related functions.
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
On other systems, it only supports IP. Functions specific for a
socket are available as methods of the socket object.
Functions:
socket() -- create a new socket object
socketpair() -- create a pair of new socket objects [*]
fromfd() -- create a socket object from an open file descriptor [*]
fromshare() -- create a socket object from data received from socket.share() [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
socket.getdefaulttimeout() -- get the default timeout value
socket.setdefaulttimeout() -- set the default timeout value
create_connection() -- connects to an address, with an optional timeout and
optional source address.
[*] not available on all platforms!
Special objects:
SocketType -- type object for socket objects
error -- exception raised for I/O errors
has_ipv6 -- boolean value indicating if IPv6 is supported
IntEnum constants:
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
Integer constants:
Many other constants may be defined; these may be used in calls to
the setsockopt() and getsockopt() methods.
CLASSES
builtins.Exception(builtins.BaseException)
builtins.OSError
gaierror
herror
timeout
builtins.object
_socket.socket
socket
enum.IntEnum(builtins.int, enum.Enum)
AddressFamily
SocketKind
class AddressFamily(enum.IntEnum)
| An enumeration.
|
| Method resolution order:
| AddressFamily
| enum.IntEnum
| builtins.int
| enum.Enum
| builtins.object
|
| Data and other attributes defined here:
|
| AF_APPLETALK = <AddressFamily.AF_APPLETALK: 16>
|
| AF_INET = <AddressFamily.AF_INET: 2>
|
| AF_INET6 = <AddressFamily.AF_INET6: 23>
|
| AF_IPX = <AddressFamily.AF_IPX: 6>
|
| AF_IRDA = <AddressFamily.AF_IRDA: 26>
|
| AF_SNA = <AddressFamily.AF_SNA: 11>
|
| AF_UNSPEC = <AddressFamily.AF_UNSPEC: 0>
|
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.Enum:
|
| name
| The name of the Enum member.
|
| value
| The value of the Enum member.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.EnumMeta:
|
| __members__
| Returns a mapping of member name->value.
|
| This mapping lists all enum members, including aliases. Note that this
| is a read-only view of the internal mapping.
class SocketKind(enum.IntEnum)
| An enumeration.
|
| Method resolution order:
| SocketKind
| enum.IntEnum
| builtins.int
| enum.Enum
| builtins.object
|
| Data and other attributes defined here:
|
| SOCK_DGRAM = <SocketKind.SOCK_DGRAM: 2>
|
| SOCK_RAW = <SocketKind.SOCK_RAW: 3>
|
| SOCK_RDM = <SocketKind.SOCK_RDM: 4>
|
| SOCK_SEQPACKET = <SocketKind.SOCK_SEQPACKET: 5>
|
| SOCK_STREAM = <SocketKind.SOCK_STREAM: 1>
|
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.Enum:
|
| name
| The name of the Enum member.
|
| value
| The value of the Enum member.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.EnumMeta:
|
| __members__
| Returns a mapping of member name->value.
|
| This mapping lists all enum members, including aliases. Note that this
| is a read-only view of the internal mapping.
SocketType = class socket(builtins.object)
| socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) -> socket object
|
| Open a socket of the given type. The family argument specifies the
| address family; it defaults to AF_INET. The type argument specifies
| whether this is a stream (SOCK_STREAM, this is the default)
| or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
| specifying the default protocol. Keyword arguments are accepted.
| The socket is created as non-inheritable.
|
| A socket object represents one endpoint of a network connection.
|
| Methods of socket objects (keyword arguments not allowed):
|
| _accept() -- accept connection, returning new socket fd and client address
| bind(addr) -- bind the socket to a local address
| close() -- close the socket
| connect(addr) -- connect the socket to a remote address
| connect_ex(addr) -- connect, return an error code instead of an exception
| dup() -- return a new socket fd duplicated from fileno()
| fileno() -- return underlying file descriptor
| getpeername() -- return remote address [*]
| getsockname() -- return local address
| getsockopt(level, optname[, buflen]) -- get socket options
| gettimeout() -- return timeout or None
| listen([n]) -- start listening for incoming connections
| recv(buflen[, flags]) -- receive data
| recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
| recvfrom(buflen[, flags]) -- receive data and sender's address
| recvfrom_into(buffer[, nbytes, [, flags])
| -- receive data and sender's address (into a buffer)
| sendall(data[, flags]) -- send all data
| send(data[, flags]) -- send data, may not send all of it
| sendto(data[, flags], addr) -- send data to a given address
| setblocking(0 | 1) -- set or clear the blocking I/O flag
| setsockopt(level, optname, value[, optlen]) -- set socket options
| settimeout(None | float) -- set or clear the timeout
| shutdown(how) -- shut down traffic in one or both directions
| if_nameindex() -- return all network interface indices and names
| if_nametoindex(name) -- return the corresponding interface index
| if_indextoname(index) -- return the corresponding interface name
|
| [*] not available on all platforms!
|
| Methods defined here:
|
| __del__(...)
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __repr__(self, /)
| Return repr(self).
|
| bind(...)
| bind(address)
|
| Bind the socket to a local address. For IP sockets, the address is a
| pair (host, port); the host must refer to the local host. For raw packet
| sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
|
| close(...)
| close()
|
| Close the socket. It cannot be used after this call.
|
| connect(...)
| connect(address)
|
| Connect the socket to a remote address. For IP sockets, the address
| is a pair (host, port).
|
| connect_ex(...)
| connect_ex(address) -> errno
|
| This is like connect(address), but returns an error code (the errno value)
| instead of raising an exception when an error occurs.
|
| detach(...)
| detach()
|
| Close the socket object wi