1
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 步:客户端将发送回一个字节字符串,但我不知道如何在此处捕获回复字节字符串。

4

1 回答 1

1

如果服务器回复发件人的 IP 地址和端口,您可以执行此操作

print('waiting Reply Bytes String from client..') //how to get the reply bytes  string from Client side?
bytes = socket.recvfrom(1024);
于 2013-01-31T12:42:32.243 回答