• 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     
  • 相关阅读:
    ATS项目更新(1) CC视图与备份路径同步
    IP地址的正则表达式
    python下载图片(2)
    NS2网络模拟(2)丢包率
    NS2网络模拟(7)homework03.tcl
    python 教程 第二十二章、 其它应用
    python 教程 第十九章、 图形界面编程
    python中string的操作函数
    python中对文件、文件夹的操作
    HTTP状态码(HTTP Status Code)
  • 原文地址:https://www.cnblogs.com/endenvor/p/14049034.html
Copyright © 2020-2023  润新知