PYTHON 3 X UDP通訊時間戳伺服器

2021-10-04 19:49:26 字數 876 閱讀 2693

建立乙個udp伺服器,然後等待udp連線,並將連線的客戶端發來的資訊,加個時間撮發揮給客戶端。

from socket import *

from time import ctime

host = '127.0.0.1'

port = 21567

bufsiz = 1024

addr = (host,port)

udpsersock = socket(af_inet,sock_dgram)

udpsersock.bind(addr)

while true:

print('waiting for message...')

data,addr = udpsersock.recvfrom(bufsiz)

print('connected:',addr,data)

sendmsg = '[%s]' % (ctime())

udpsersock.sendto('[%s] %s'.encode("utf-8") % (ctime().encode(),data),addr)

print('...received from and returned to:',addr)

udpsersock.close()

我用網路助手傳送乙個訊息給這個udp服務,結果收到的資訊

[2020-04-06 22:37:54.884]# send ascii to 127.0.0.1 :21567>

hello world!

[2020-04-06 22:37:55.009]# recv ascii from 127.0.0.1 :21567>

[mon apr  6 22:37:54 2020] hello world!

python3時間 Python3 時間處理

usr bin python3 import time 引入time模組 python3 日期和時間 當前時間戳 ticks time.time print 當前時間戳為 ticks 當前時間戳為 1522024006.313911 完全版本地時間 localtime time.localtime ...

python3藍芽通訊 python實現藍芽通訊

安裝和示例 linux下安裝 sudo apt get install python pip libglib2.0 dev sudo pip install bluepy 官方示例 importbtleclassmydelegate btle.defaultdelegate def init sel...

python3之路 基礎 時間

在敲 過程中,我們難免會涉及到時間的轉換,那麼python3中的時間是如何轉換的呢?author gpf 功能 import time 獲取當前時間 返回float,單位為秒 defgetnowtime return time.time 獲取當前時間 返回tuple defgettuplelocal...