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

Python网络编程UDP服务器与客服端简单例子

时间:2018-04-17 11:53:23      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:recvfrom   ret   bind   host   ati   details   sdn   tail   message   

[转载] https://blog.csdn.net/hu330459076/article/details/7868028

 

UDP服务器代码:

#!/usr/bin/env python
# -*- coding:UTF-8 -*-

from socket import *
from time import ctime

HOST = 127.0.0.1
PORT = 21567
BUFSIZE = 1024

ADDR = (HOST,PORT)

udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADDR)

while True:
    print wating for message...
    data, addr = udpSerSock.recvfrom(BUFSIZE)
    udpSerSock.sendto([%s] %s%(ctime(),data),addr)
    print ...received from and retuned to:,addr

udpSerSock.close()


 

UDP客服端代码:

#!/usr/bin/env python

from socket import *

HOST = localhost
PORT = 21567
BUFSIZE = 1024

ADDR = (HOST, PORT)

udpCliSock = socket(AF_INET, SOCK_DGRAM)

while True:
    data = raw_input(>)
    if not data:
        break
    udpCliSock.sendto(data,ADDR)
    data,ADDR = udpCliSock.recvfrom(BUFSIZE)
    if not data:
        break
    print data

udpCliSock.close()

 

Python网络编程UDP服务器与客服端简单例子

标签:recvfrom   ret   bind   host   ati   details   sdn   tail   message   

原文地址:https://www.cnblogs.com/baby0814/p/8861778.html

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