In python, I convert a dictionary to a json string, use standard python encoding, then use base64 to further encode for sending over a socket like this.
item.data is a list of dicts. myconverter is there to handle datetime.
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
print("UDP target IP:", UDP_IP)
print("UDP target port:", UDP_PORT)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
def myconverter(o):
if isinstance(o, datetime.datetime):
return o.__str__()
async def main_loop()
....
async for item in streamer.listen():
for index, quoteDict in enumerate(item.data):
quote = json.dumps(quoteDict, default = myconverter)
sock.sendto(base64.b64encode(quote.encode('ascii')), (UDP_IP, UDP_PORT))
When I use python to get the data sent over a socket like this, everything works fine:
import socket
import json
import base64
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(16384) # buffer size is 16384 bytes
quote_dict = base64.b64decode(data)
print(quote_dict)
What is the equivalent section of C# code that decodes the python enconded datagram above?