UDP服务器需要做的事情比较少, 除了等待传入的连接之外, 几乎不需要做其他工作.

参考下面的代码:

from socket import *

host = "localhost" # 服务器要绑定的地址
port = 20000 # 服务器要绑定的端口
address = (host, port)

bufSize = 1024

udpServeSock = socket(AF_INET, SOCK_DGRAM)
udpServeSock.bind(address)

while True:
    print("等待有人给我发信息:")
    data, cliAddress = udpServeSock.recvfrom(bufSize)
    print(cliAddress, "发来的信息是:", data.decode("utf-8"))

udpServeSock.close()
Copyright © 李振超 2018 all right reserved,powered by Gitbook
该文件最后修订时间: 2018-02-25 07:12:09

results matching ""

    No results matching ""