UDP simultaneous connection with Python

In order to stream video data, we first created a program that can send and receive simple number data!

Why you chose UDP

At first, I made a streaming server with TCP, but it seemed that it would not be possible to communicate with multiple clients at the same time with TCP, so I decided to make it with UDP, which is a stateless protocol!

setting file

connection.ini


[server]
ip = 127.0.0.1
port = 1935

[packet]
# [bytes]
header_size = 4
# [pixels]
image_width = 256
image_height = 256

Receiver

server.py


import socket
import configparser
import logging

logging.basicConfig(level=logging.DEBUG)

config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')

# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))

# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))

if __name__ == '__main__':
  with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.bind((SERVER_IP, SERVER_PORT))
    logging.info(" Binding port on: " + SERVER_IP + ":" + str(SERVER_PORT))

    while True:
      data, addr = s.recvfrom(IMAGE_SIZE)
      print(addr, int.from_bytes(data, 'big'))

Sender

stream.py


import socket
import configparser
import logging
import time

logging.basicConfig(level=logging.DEBUG)

config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')

# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))

# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))

if __name__ == '__main__':
  with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    i = 0
    while True:
      s.sendto(i.to_bytes(IMAGE_SIZE, 'big'), (SERVER_IP, SERVER_PORT))
      logging.info(" Sent: " + str(i))
      time.sleep(1)
      i = i + 1

Rough flow

It reads the address and port from the config file and continues to send the ever-incremented ʻi value from stream.pyto'server.py'. 'socket.SOCK_DGRAM' indicates that UDP communication is performed.recvfrom ()` returns the received data and the tuple of the data source, which is used to display the information in the terminal.

Execution result

Try running it with one server.py and two stream.py.

INFO:root: IMAGE SIZE: 32768
INFO:root: Binding port on: 127.0.0.1:1935
('127.0.0.1', 55382) 0
('127.0.0.1', 55382) 1
('127.0.0.1', 55382) 2
('127.0.0.1', 55382) 3
('127.0.0.1', 55382) 4
('127.0.0.1', 55382) 5
('127.0.0.1', 55382) 6
('127.0.0.1', 55382) 7
('127.0.0.1', 55382) 8
('127.0.0.1', 59979) 0
('127.0.0.1', 55382) 9
('127.0.0.1', 59979) 1
('127.0.0.1', 55382) 10
('127.0.0.1', 59979) 2
('127.0.0.1', 55382) 11
('127.0.0.1', 59979) 3
('127.0.0.1', 55382) 12
('127.0.0.1', 59979) 4
('127.0.0.1', 55382) 13
('127.0.0.1', 59979) 5
('127.0.0.1', 55382) 14
('127.0.0.1', 59979) 6
('127.0.0.1', 55382) 15
('127.0.0.1', 59979) 7
('127.0.0.1', 55382) 16
('127.0.0.1', 59979) 8
('127.0.0.1', 55382) 17
('127.0.0.1', 59979) 9
('127.0.0.1', 55382) 18
('127.0.0.1', 59979) 10
('127.0.0.1', 55382) 19
('127.0.0.1', 59979) 11

Data is being sent from each of the two addresses!

Recommended Posts

UDP simultaneous connection with Python
Connection pooling with Python + MySQL
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Debug for mysql connection with python mysql.connector
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
ODBC connection to FileMaker 11 Server Advanced with Python 3
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
python> udp> echo server
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python