?本示例使用的設備:RFID網(wǎng)絡WIFI無線TCP/UDP/HTTP可編程二次開發(fā)讀卡器POE供電語音-淘寶網(wǎng) (taobao.com)文章來源地址http://www.zghlxwxcb.cn/news/detail-655036.html
# -*- coding: utf-8 -*-
import time
import datetime
import socket
import threading
#將中文信息轉(zhuǎn)換編碼,顯示文字、TTS語音都需要轉(zhuǎn)換--------------------------------------------------
def GetChineseCode(inputstr):
sdata = bytes(inputstr, encoding='gbk') # 將中文信息轉(zhuǎn)為bytes
hexcode=""
for num in range(0, len(sdata)):
if num % 2==0:
hexcode=hexcode+"\\x"
hexcode=hexcode+ '%02X' % (sdata[num])
return hexcode
#獲取電腦系統(tǒng)日期時間---------------------------------------------------------------------------
def get_time():
st = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
st=st[2:19]
return st
# 接收讀卡器發(fā)過來的http請求、解析提交上來的信息、回應并驅(qū)動讀卡器顯示文字播報語音--------------------------
def service_client(new_socket):
request = new_socket.recv(1024).decode('utf-8')
request_header_lines = request.splitlines()
requestlines=len(request_header_lines)
current_time = datetime.datetime.now()
current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
print(current_time_str)
i = 0
while i < requestlines: #打印出提交信息
print(request_header_lines[i])
i += 1
print("\n")
if request[0:3]=="GET":
CommitParameter=request_header_lines[0][request_header_lines[0].find("?")+1:request_header_lines[0].find("HTTP/1.1")-1]
elif request[0:4]=="POST":
CommitParameter = request_header_lines[requestlines-1]
if request_header_lines[5]=="Content-Type: application/json":
CommitParameter = CommitParameter.replace("{", "") #JSON信息可以引用JSON類來解析,此處統(tǒng)一轉(zhuǎn)化成字符串處理
CommitParameter = CommitParameter.replace("\"", "")
CommitParameter = CommitParameter.replace(":", "=")
CommitParameter = CommitParameter.replace(",", "&")
CommitParameter = CommitParameter.replace("}", "")
FieldsList = CommitParameter.split('&')
for num in range(0, len(FieldsList)):
ParaList=FieldsList[num].split('=')
if ParaList[0]=="info":
info=ParaList[1] #接收到的數(shù)據(jù)包號,需回應該包號
elif ParaList[0]=="jihao":
jihao = ParaList[1] #設備機號(可自編)
elif ParaList[0]=="cardtype":
cardtype = ParaList[1]
typenum=int(cardtype,16) % 16 #typenum=1 ID卡,2 HID卡,3 T5557卡,4 EM4305卡,5 IC卡,6 二代身份證,7 是15693卡,IClass"
pushortake=int(int(cardtype,16) / 128) #pushortake=0 表示讀卡,>0表示卡離開感應區(qū)
elif ParaList[0]=="card":
card = ParaList[1] #接收到的原始16進制卡號,可根據(jù)需要自行轉(zhuǎn)換成其他卡號
elif ParaList[0]=="data":
data = ParaList[1] #讀取的卡扇區(qū)內(nèi)容
elif ParaList[0]=="dn":
dn = ParaList[1] #設備硬件序列號,出廠時已固化,全球唯一
elif ParaList[0]=="status":
status = ParaList[1] #讀卡狀態(tài),如密碼認證失敗為12
if pushortake == 0:
ChineseVoice = GetChineseCode("讀取卡號[n1]") + card
else:
ChineseVoice = GetChineseCode("卡號[n1]") + card + GetChineseCode("離開感應區(qū)")
# Response=1,是固定的回應頭信息+接收到的包序號+顯示文字 中文要轉(zhuǎn)換編碼 {}中的文字可以高亮顯示+顯示延時秒+蜂鳴器響聲代碼+[v8]表示本次語音大小 取值 v1 到 v16 TTS中文語音編碼
ResponseStr="Response=1,"+info+",{"+GetChineseCode("卡號")+":}"+(card+" ")[0:12]+get_time()+",20,1,[v8]"+ChineseVoice
new_socket.send(ResponseStr.encode("gbk"))
new_socket.close()
print(ResponseStr+"\r\n")
def main():
# 用來完成整體的控制
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 1.創(chuàng)建套接字
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 設置當服務器先close 即服務器端4次揮手之后資源能夠立即釋放,這樣就保證了,下次運行程序時 可以立即綁定設定的端口
tcp_server_socket.bind(("", 88)) # 2.綁定監(jiān)聽端口
tcp_server_socket.listen(128) # 3.變?yōu)楸O(jiān)聽套接字
while True:
new_socket, client_addr = tcp_server_socket.accept() # 4.等待新客戶端的鏈接
t = threading.Thread(target=service_client, args=(new_socket,)) # 5.為這個客戶端服務
t.start()
tcp_server_socket.close() # 關閉監(jiān)聽套接字
if __name__ == '__main__':
main()
文章來源:http://www.zghlxwxcb.cn/news/detail-655036.html
到了這里,關于Python實現(xiàn)輕量級WEB服務器接收HTTP提交的RFID刷卡信息并回應驅(qū)動讀卡器顯示播報語音的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!