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

python3 udp socketserver

时间:2019-06-17 17:09:27      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:inpu   thread   服务   request   enc   threading   reading   __name__   hand   

# -*- coding: utf-8 -*-
from socket import *


server = socket(AF_INET, SOCK_DGRAM)  # udp以数据报的形式传输数据
ip_port = ("127.0.0.1", 8001)  # 服务端的IP和端口
server.bind(ip_port)  # 绑定ip和端口

while 1:  # 通讯循环
    client_msg, client_addr = server.recvfrom(1024)
    print("from client %s msg: %s" % (client_msg, client_addr))
    server.sendto(client_msg.upper(), client_addr)
# -*- coding: utf-8 -*-
from socket import *


client = socket(AF_INET, SOCK_DGRAM)
server_addr = ("127.0.0.1", 8001)

while 1:  # 通讯循环
    inp = input(">>:").strip()
    client.sendto(inp.encode(), server_addr)
    server_msg, server_addr = client.recvfrom(1024)
    print("from server %s msg: %s" % (server_msg, server_addr))
# -*- coding: utf-8 -*-
import socketserver


class MyUDPhandler(socketserver.BaseRequestHandler):
    def handle(self):
        client_msg, server = self.request
        server.sendto(client_msg.upper(), self.client_address)

if __name__ == __main__:
    ip_port = ("127.0.0.1", 8001)  # 服务端的IP和端口
    server = socketserver.ThreadingUDPServer(ip_port, MyUDPhandler)
    server.serve_forever()

 

python3 udp socketserver

标签:inpu   thread   服务   request   enc   threading   reading   __name__   hand   

原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11040570.html

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