import socketserver
import struct
from collections import namedtuple
class MyUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
if len(data) >= 34:
format_ = "6shhih50s2s"
MyStruct = namedtuple("MyStruct", "sMark nPackLen nFlag nGisIp nPort sData sEnd")
tuple_to_send = MyStruct(sMark="\r\n{}".format("*KW").encode("ascii"),
nPackLen=struct.calcsize(format_),
nFlag=0x0002,
nGisIp=0,
nPort=0,
sData= "*KW,AA05954122,015,080756,#".encode("ascii"),
sEnd="\r\n".encode("ascii"))
string_to_send = struct.pack(format_, *tuple_to_send._asdict().values())
socket.sendto(string_to_send, self.client_address)
print('waiting Reply Bytes String from client..') //how to get the reply bytes string from Client side?
if __name__ == "__main__":
HOST, PORT = "", 6903
server = socketserver.UDPServer((HOST, PORT), MyUDPHandler)
server.serve_forever()
第1步:服务器接收GPS设备字节字符串,并获取动态IP地址
步骤2 :当服务器已经获得客户端动态IP时,服务器将向客户端(GPS设备)发送命令。
第3步:客户端会发回一个字节字符串,但我不知道如何在这里捕获回复字节字符串。
答案 0 :(得分:1)
如果服务器回复发件人的IP地址和端口
,则可以执行此操作print('waiting Reply Bytes String from client..') //how to get the reply bytes string from Client side?
bytes = socket.recvfrom(1024);