Skip to content

Commit 709aec2

Browse files
authored
练习UDP网络协议
1 parent fb69443 commit 709aec2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test51.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'练习UDP网络协议'
5+
6+
__author__ = 'sergiojune'
7+
import socket
8+
9+
# 此文件用于当服务器
10+
# 创建socket
11+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 第二个参数为指定udp协议
12+
# 绑定端口,不需要监听
13+
s.bind(('127.0.0.1', 9999))
14+
print('await connect')
15+
while True:
16+
data, addr = s.recvfrom(1024) # 这个直接返回客户端的ip和请求信息
17+
print('connect form %s:%s' % addr)
18+
# 发送数据回客户端
19+
s.sendto(b'hello %s' % data, addr) # 第二个参数为发送到的ip

0 commit comments

Comments
 (0)