Python Forum
UDP to TCP requests script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UDP to TCP requests script
#1
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
Reply


Messages In This Thread
UDP to TCP requests script - by batchenr - Jun-30-2019, 07:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Configuring requests module to use secondary IP on server for outbound requests mohit 1 6,567 Oct-24-2016, 05:21 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020