本示例使用的设备:RFID网络WIFI无线TCP/UDP/HTTP可编程二次开发读卡器POE供电语音-淘宝网 (taobao.com)
python
# -*- coding: utf-8 -*-
import time
import datetime
import socket
import threading
#将中文信息转换编码,显示文字、TTS语音都需要转换--------------------------------------------------
def GetChineseCode(inputstr):
sdata = bytes(inputstr, encoding='gbk') # 将中文信息转为bytes
hexcode=""
for num in range(0, len(sdata)):
if num % 2==0:
hexcode=hexcode+"\\x"
hexcode=hexcode+ '%02X' % (sdata[num])
return hexcode
#获取电脑系统日期时间---------------------------------------------------------------------------
def get_time():
st = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
st=st[2:19]
return st
# 接收读卡器发过来的http请求、解析提交上来的信息、回应并驱动读卡器显示文字播报语音--------------------------
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类来解析,此处统一转化成字符串处理
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] #接收到的数据包号,需回应该包号
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表示卡离开感应区
elif ParaList[0]=="card":
card = ParaList[1] #接收到的原始16进制卡号,可根据需要自行转换成其他卡号
elif ParaList[0]=="data":
data = ParaList[1] #读取的卡扇区内容
elif ParaList[0]=="dn":
dn = ParaList[1] #设备硬件序列号,出厂时已固化,全球唯一
elif ParaList[0]=="status":
status = ParaList[1] #读卡状态,如密码认证失败为12
if pushortake == 0:
ChineseVoice = GetChineseCode("读取卡号[n1]") + card
else:
ChineseVoice = GetChineseCode("卡号[n1]") + card + GetChineseCode("离开感应区")
# Response=1,是固定的回应头信息+接收到的包序号+显示文字 中文要转换编码 {}中的文字可以高亮显示+显示延时秒+蜂鸣器响声代码+[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.创建套接字
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 设置当服务器先close 即服务器端4次挥手之后资源能够立即释放,这样就保证了,下次运行程序时 可以立即绑定设定的端口
tcp_server_socket.bind(("", 88)) # 2.绑定监听端口
tcp_server_socket.listen(128) # 3.变为监听套接字
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() # 关闭监听套接字
if __name__ == '__main__':
main()