1. 首页
  2. 移动开发
  3. 其他
  4. Python多线程实现06UDP客户端案例

Python多线程实现06UDP客户端案例

上传者: 2023-03-10 06:45:07上传 PY文件 950B 热度 12次
# Python多线程实现06UDP客户端案例

这个案例演示了使用Python多线程来实现06UDP客户端。

## 代码

import threading

import socket

def udp_client(ip, port):

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True:

message = input('请输入要发送的消息:')

if not message:

break

client.sendto(message.encode('utf-8'), (ip, port))

client.close()

if name == 'main':

ip = '127.0.0.1'

port = 8080

t = threading.Thread(target=udp_client, args=(ip, port))

t.start()

t.join()

下载地址
用户评论