|
本次使用的主要模块是socket模块,在这个模块中可以找到socket()函数,该函数用于创建套接字对象。套接字也有自己的方法集,这些方法可以实现基于套接字的网络通信。
socket()模块函数
要创建套接字,必须使用socket.socket()函数,语法如下:
socket(socket_family,socket_type,protocol=0)
其中socket_family是AF_UNIX(基于文件)或AF_INET(面向网络),socket_type是SOCK_STREAM(TCP)或SOCK_DGRAM(UDP),protocol通常省略,默认为0
示例:创建TCP/IP套接字:
tcpSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
创建UDP/IP套接字:
udpSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
创建TCP时间戳服务器和客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| # 服务器
from socket import *
from time import ctime
host = '' # 为空表示可以使用任何可用地址
port = 21567 # 监听端口
bufsiz = 1024 # 缓冲区大小
addr = (host,port)
tcpSerSock = socket(AF_INET,SOCK_STREAM) # 分配TCP服务器套接字
tcpSerSock.bind(addr) # 绑定服务器地址
tcpSerSock.listen(5) # 开启TCP监听器调用
try:
while True: # 进入循环,等待客户端连接
print 'waiting for connection...'
tcpCliSock,addr = tcpSerSock.accept() # 出现连接请求
print '...connection from:',addr
while True:
data = tcpCliSock.recv(bufsiz) # 获取消息
if not data: # 消息为空,就退出
break
tcpCliSock.send('[%s] %s'%(ctime(),data)) # 消息存在,则加上时间戳并返回数据
tcpCliSock.close()
except KeyboardInterrupt: # ctrl+c退出
print 'you have CTRL+C,quit now'
tcpSerSock.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| # 客户端
from socket import *
host = 'localhost' # host,port为服务器主机名和端口
port = 21567
bufsiz = 1024
addr = (host,port)
tcpCliSock = socket(AF_INET,SOCK_STREAM) # 分配TCP客户端套接字
tcpCliSock.connect(addr) # 主动调用并连接服务器
try:
while True:
data = raw_input('> ') # 输入字符串
if not data:
break
tcpCliSock.send(data) # 发送到服务器
data = tcpCliSock.recv(bufsiz) # 获取服务器数据
if not data:
break
print data
except KeyboardInterrupt:
print 'you have CTRL+C,quit now'
tcpCliSock.close()
|
执行TCP服务器和客户端
1
2
3
4
5
6
7
| # 客户端
[iyunv@test python]# python tcp_client.py
> hi
[Thu Aug 11 11:11:04 2016] hi
> welcome!
[Thu Aug 11 11:11:10 2016] welcome!
>
|
1
2
3
4
5
| # 服务器端
[iyunv@test python]# python tcp_server.py
waiting for connection...
...connection from: ('127.0.0.1', 52900)
waiting for connection...
|
创建UDP时间戳服务器和客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| # 服务器
from socket import *
from time import ctime
host = ''
port = 21567
bufsiz = 1024
addr = (host,port)
udpSerSock = socket(AF_INET,SOCK_DGRAM) # 分配UDP服务器套接字
udpSerSock.bind(addr) # UDP是无连接的,所以不需要监听
try:
while True:
print 'waiting for message...'
data,addr = udpSerSock.recvfrom(bufsiz) # 获取消息,处理并返回客户端
udpSerSock.sendto('[%s] %s'%(ctime(),data),addr)
print '...received from and return to:',addr
except KeyboardInterrupt:
print 'you have CTRL+C,quit now'
udpSerSock.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # 客户端
from socket import *
host = 'localhost'
port = 21567
bufsiz = 1024
addr = (host,port)
udpCliSock = socket(AF_INET,SOCK_DGRAM) # 分配UDP套接字
try:
while True: # 工作方式几乎与TCP客户端一样
data = raw_input('> ')
if not data:
break
udpCliSock.sendto(data,addr)
data,addr = udpCliSock.recvfrom(bufsiz)
if not data:
break
print data
except KeyboardInterrupt:
print 'you have CTRL+C,quit now'
udpCliSock.close()
|
执行结果
1
2
3
4
5
6
7
| # 客户端
[iyunv@test python]# python udp_client.py
> 123
[Thu Aug 11 11:23:01 2016] 123
> hello
[Thu Aug 11 11:23:04 2016] hello
>
|
1
2
3
4
5
6
7
| # 服务器端
[iyunv@test python]# python udp_server.py
waiting for message...
...received from and return to: ('127.0.0.1', 55532)
waiting for message...
...received from and return to: ('127.0.0.1', 55532)
waiting for message...
|
|
|