参考
- python 官方socket文档
- Python核心编程
Socket family
- AF_INET a pair(host,port) is used for the address family, where host is a string representing either a host name in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like'100.50.200.5', and port is an integer.
constants
- socket.AF_UNIX, socket.AF_INET, socket.AF_INET6
- socket.SOCK_STRESM, socket.SOCK_DGRAM, socket.SOCK_RAW, socket.SOCK_RDM, socket.SOCK_SEQPACKET
- socket.SOCK_CLOEXEC, socket.SOCK_NONBLOCK
分别对应socket()函数的三个参数.
functions
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Create a new socket using the given address family, socket type and protocol number.
Socket object
socket.accept()
Accept a connection.The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
socket.bind(address)
Bind the socket to address. The socket must not already be bound. (The format of address depends on the address family).
socket.close()
Mark the socket closed.
socket.connect(address)
Connect to a remote socket at address. (The format of address depends on the address family).
socket.listen([backlog])
Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections.
socket.recv(bufsize[, flags])
Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero.
Note For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096.
socket.send(bytes[, flags])
Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above.
TCP server
from time import sleep, ctime
import socket
HOST = ''
port = 12345
BUFSIZE = 1024
ADDR = (HOST, port)
tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print 'waiting for connection...'
tcpCliSock, addr = tcpSerSock.accept()
print '...connected from:', addr
while True:
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
tcpCliSock.send('[%s] %s' %(ctime(), data))
tcpCliSock.close()
tcpSerSock.close()
TCP client
import socket
HOST = 'localhost'
PORT = 21456
BUFSIZE = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data = raw_input(">")
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
print data
tcpCliSock.close()
UDP server
from socket import *
from time import ctime
HOST = ''
PORT = 21456
BUFSIZE = 1024
ADDR = (HOST, PORT)
udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADDR)
while True:
print 'waiting for message...'
data, addr = udpSerSock.recvfrom(BUFSIZE)
udpSerSock.sendto('[%s] %s' % (ctime(), data), addr)
print '...recerived from and returned to:', addr
udpSerSock.close()
UDP client
from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
udpCliSock = socket(AF_INET, SOCK_DGRAM)
while True:
data = raw_input(">")
if not data:
break
udpCliSock.sendto(data, ADDR)
data, ADDR = udpCliSock.recvfrom(BUFSIZE)
if not data:
break
print data
udpCliSock.close()
网友评论