'Replace c# Serialize to python
I have a very specific problem and I hope that I get some direction for a solution, first I am not a guy of C# And I have no prior knowledge of this language. My mission is to replace some code (C#) and write this in python. the code makes serialization to some data and sends this struct in UDP. On the other side the client takes the data and makes deserialize still in c# this side I not going to replace and I can't replace. In python, I use pickle to make serialization and I send tuple, unfortunately, it's not working I see a difference in Wireshark when I send from C# code the length of the message equal to 1264 bytes and from python 175 bytes. Would appreciate help..
Example C# serialize (I want replace this part):
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace VMSProxyClient
{
[Serializable]
public class TelemetryUdpPacket //OCU to VMS and VMS to OCU
{
public bool IsToVMS { get; set; }
public long Timestamp { get; set; }
public bool IsDay { get; set; }
public bool IsDprOk { get; set; }
public bool IsLocked { get; set; }
public int Quality { get; set; }
public bool IsSendTo { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public double AboveSeaLevel { get; set; }
public double Yaw { get; set; }
public double Pitch { get; set; }
public double Roll { get; set; }
public double GimbalPitch { get; set; }
public double GimbalRoll { get; set; }
public double HorizontalFov { get; set; }
public double VerticalFov { get; set; }
public PacketPoint3D FootprintCenter { get; set; }
public PacketPoint3D FootprintTopLeft { get; set; }
public PacketPoint3D FootprintTopRight { get; set; }
public PacketPoint3D FootprintBottomRight { get; set; }
public PacketPoint3D FootprintBottomLeft { get; set; }
public List<string> MapList { get; set; }
public static TelemetryUdpPacket Deserialize(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
var formatter = new BinaryFormatter();
var result = (TelemetryUdpPacket)formatter.Deserialize(stream);
stream.Close();
return result;
}
}
public static byte[] Serialize(TelemetryUdpPacket packet)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, packet);
return stream.ToArray();
}
}
}
}
my python code :
import websocket
import json
import pickle
import ipaddress
from typing import NamedTuple
import socket
class TelemetryUdpPacket(NamedTuple):
IsToVMS: bool
Timestamp: float
IsDay: bool
IsDprOk: bool
IsLocked: bool
Quality: int
IsSendTo: bool
Longitude: float
Latitude: float
AboveSeaLevel: float
Yaw: float
Pitch: float
Roll: float
GimbalPitch: float
GimbalRoll: float
HorizontalFov: float
VerticalFov: float
FootprintCenter: list
FootprintTopLeft: list
FootprintTopRight: list
FootprintBottomRight: list
FootprintBottomLeft: list
MapList: str
class ProxyServer:
def __init__(self, PORT=8090, IP='11.11.11.4'):
self.PORT = PORT
self.IP = ipaddress.ip_address(IP)
print(f"STARTING WBSOCKET = ws://{self.IP}:{self.PORT}/Protrack-
Locator/Data/LocationResults")
self.opened_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Start
UDP to send buffer to ProxyClient
self.opened_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #
Addition command to close socket port immediately
self.opened_socket.bind(("11.11.11.6", 40005)) # Initalize my local: IP, PORT
wsapp = websocket.WebSocketApp(f"ws://{self.IP}:{self.PORT}/Protrack-
Locator/Data/LocationResults",
on_message=self.on_message)
wsapp.run_forever()
def SendUDP(self, buffer, IP='11.11.11.5', PORT=40006):
try:
self.opened_socket.sendto(buffer, (IP, PORT))
return 1
except Exception as e:
print(f"Error in SendUDP Function: {e}")
return -1
def on_message(self, wsapp, message):
# first parsing data from locator
message = json.loads(message.decode('utf-8'))
# Sort data before sending to Proxy client
packet = TelemetryUdpPacket(False, message['Time']['MsgEpochTimestamp'], bool(message['CameraModel']['Type']),
True,
True, message['State']['LockSate'], True, message['SelfPosition']['Lon'],
message['SelfPosition']['Lat'],
message['SelfPosition']['Alt'], message['CameraModel']['Heading'],
message['CameraModel']['Pitch'],
message['CameraModel']['Roll'], 0, 0, message['CameraModel']['HorzFov'],
message['CameraModel']['VertFov'],
[message['MapFootprint']['Center']['Lon'], message['MapFootprint']['Center']['Lat'],
message['MapFootprint']['Center']['Alt']],
[message['MapFootprint']['TopLeft']['Lon'],
message['MapFootprint']['TopLeft']['Lat'],
message['MapFootprint']['TopLeft']['Alt']],
[message['MapFootprint']['TopRight']['Lon'],
message['MapFootprint']['TopRight']['Lat'],
message['MapFootprint']['TopRight']['Alt']],
[message['MapFootprint']['BottomRight']['Lon'],
message['MapFootprint']['BottomRight']['Lat'],
message['MapFootprint']['BottomRight']['Alt']],
[message['MapFootprint']['BottomLeft']['Lon'],
message['MapFootprint']['BottomLeft']['Lat'],
message['MapFootprint']['BottomLeft']['Alt']],
"API_is_Working!")
# Now we make Serializable in python
buff = pickle.dumps(packet)
self.UDPCheck = self.SendUDP(buff)
x = ProxyServer()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
