zoukankan      html  css  js  c++  java
  • python udp time demo

     1 import socket
     2 import time
     3 
     4 if __name__ == '__main__':
     5     mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     6     host_port = ("127.0.0.1", 9527)
     7     mysocket.bind(host_port)
     8     #input("wait for msg: ")
     9     while True:
    10         print('waitting for request')
    11         data, addr = mysocket.recvfrom(1024)
    12         print("request type: {}, from: {}".format(data.decode(), addr))
    13         response = ''
    14         if data.decode() == 'time':
    15             response = time.strftime("%H:%M:%S", time.localtime())
    16         elif data.decode() == 'date':
    17             response = time.strftime("%Y-%m-%d", time.localtime())
    18         elif data.decode() == 'datetime':
    19             response = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    20         elif data.decode() == 'quit':
    21             response = "server quit"
    22         else:
    23             response = "invalid request"
    24         mysocket.sendto(response.encode(), addr)
    25         if data.decode() == 'quit':
    26             exit(0)
     1 import socket
     2 
     3 def myinput():
     4      while True:
     5         myinput = input("input time or date: ")
     6         if not myinput:
     7             print("empty input")
     8             continue
     9         if myinput == 'time':
    10             return 'time'
    11         if myinput == 'date':
    12             return 'date'
    13         if myinput == 'datetime':
    14             return 'datetime'
    15         if myinput == 'quit':
    16             return 'quit'
    17         print('invalid input')
    18 
    19 if __name__=='__main__':
    20     mysocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    21     while True:
    22         choice = myinput()
    23         mysocket.sendto(choice.encode(), ('127.0.0.1', 9527))
    24         data, addr = mysocket.recvfrom(1024)
    25         print("data: {}, addr: {}".format(data.decode(), addr))
    26         if choice == 'quit':
    27             print('quit')
    28             exit(0)
    29     
  • 相关阅读:
    【GitHub】上传代码通用操作等(附下载单个文件夹或文件)
    【Git】之分支合并命令
    【FFmpeg】之Mac系统爬取所有M3U8视频下载方法
    i2c超时
    linux下串口调试
    cgminer分析
    i2c驱动理解
    STM32(三十七)SPI读取W25Q128flash的厂商ID、设备ID以及读写数据(硬件SPI)
    驱动静态和动态加载
    I2C基本原理及对I2C Adapter的理解
  • 原文地址:https://www.cnblogs.com/endenvor/p/14049034.html
Copyright © 2011-2022 走看看