码迷,mamicode.com
首页 > 编程语言 > 详细

Python UDPSocket

时间:2018-03-31 00:41:11      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:Python;UDPSocket

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

标签:Python;UDPSocket

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!