首页 > 编程语言 > 详细

Python UDPSocket

时间:2018-03-31 00:33:25      阅读:244      评论:0      收藏:0      [点我收藏+]
UDPSocketServer:

from socket import *
from time import ctime
BUFFSIZE = 1024
HOST = "127.0.0.1"
PORT = 10055
ADDR = (HOST, PORT)
mySocket = socket(AF_INET, SOCK_DGRAM, 0)
mySocket.bind(ADDR)
while True:
    print("waiting for message...\n")
    data, addr = mySocket.recvfrom(BUFFSIZE)
    data = data.decode()
    print("connection from ", data)
    if not data:
        break
    mySocket.sendto(("[%s] %s" % (ctime(), data)).encode(), addr)
mySocket.close()

UDPSocketClient:

from socket import *
BUFFSIZE = 1024
HOST = "127.0.0.1"
PORT = 10055
ADDR = (HOST, PORT)
mySocket = socket(AF_INET, SOCK_DGRAM,0)
while True:
    data = input(">: ")
    if not data:
        break
    mySocket.sendto(data.encode(), ADDR)
    data, addr = mySocket.recvfrom(BUFFSIZE)
    if not data:
        break
    print(data)
mySocket.close()

Python UDPSocket

原文:http://blog.51cto.com/superhakce/2093206

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!