hkebao
- UID
- 9880
- 帖子
- 35
- 积分
- 80
- 在线时间
- 1 小时
|
1#
hkebao 发表于 2009-02-05 09:48
python源码学习一
1.突然想起来我之前一个领导说过:研究源码是提高最快的方式!
socket.py源码学习一:
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
-
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() -- mape 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)
ssl() -- secure socket layer support (only available if configured)
socket.getdefaulttimeout() -- get the default timeout value
socket.setdefaulttimeout() -- set the default timeout value
Special objects:
SocketType -- type object for socket objects
error -- exception raised for I/O errors
has_ipv6 -- boolean value indicating if IPv6 is supported
[例如:print socket.has_ipv6 打印出来是true]
Integer constants:
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
[可以直接打印出来显示]都是显示为1 2 3
_socketmethods = (
'bind', 'connect', 'connect_ex', 'fileno', 'listen',
'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
'sendall', 'setblocking',
'settimeout', 'gettimeout', 'shutdown')
_delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
"send", "sendto")
def accept(self):
sock, addr = self._sock.accept()
return _socketobject(_sock=sock), addr
构造一个简单的SOCKET通讯机制:
1.通过SOCKET的accept()方法构造出来一个连接对象。然后这个连接对象可以通过recv与send方法来实现
发送与接受信息。
buf=connection.recv(1024) 表示来自于客户端的信息。大小 不能超过1024
构造出来这样一个原型:
用户服务器每隔10去请求一下MYSQL数据库服务器。然后将结果值发送到客户端!
代码如下:
|