Python Forum
UDP to TCP requests script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: UDP to TCP requests script (/thread-19457.html)



UDP to TCP requests script - batchenr - Jun-30-2019

Hey,

Im using python3, i have a script on the server that gets UDP requests from client and then
the server translate thous requests to tcp and transfer them internally to a py script on the server.

My question is :
My tcp_to_udp script on the server knows that the first request is always the headers and then the data.
How do i make my script know the difference between headers and data ?

I want my script to know what is data and what is headers because now it only
knows that first request is headers but this method is not so good if connection restarts..

Also if its multi connection, to know which headers is connected to which data.

my script now (on the server):

import json
import requests
import signal
import sys
import socket
from _thread import *

UDP_IP = ""
UDP_PORT = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
s.bind((UDP_IP, UDP_PORT))
print("Wating for connections")

def data_iter():
    while True:
        getInfo = data, addr = s.recvfrom(10000000024)
        print("My data:", data)
        yield(data)

headers = None
while headers is None:
    headers, addr = s.recvfrom(4024)
    if headers is not None:
        break
headers = headers.decode("utf-8")
headers = json.loads(headers)

try:
    r = requests.post('http://10.0.0.1:5001/stream', headers=headers, data=data_iter())
except socket.error as error:
    if error.errno == errno.EPIPE:
        print(os.strerror(error.errno))
    else:
        raise
data type :
headers are json type converted to bytes:
headers  = b'{"RECORDER_NAME": "USB Audio OnBoard: #1 (hw:0,1)", "LZMA": "1", "RECORDER_RATE": "44100"}'
My data: b'nanannananananna' # because im testing