Socket communication and multi-thread processing by Python

Multi-thread processing is performed, and the sub thread becomes the server and accepts the input from the main thread. After executing it on the terminal, type an appropriate character string and press the Enter key, and the character string will be sent from the main thread to the sub thread by socket communication. When the subthread receives the string, it is printed to the terminal by the print statement.

environment ・ Python2.7

import threading
import socket
import time
import datetime

# for receiving
class TestThread(threading.Thread):
    def __init__(self):
        super(TestThread, self).__init__()
        = ""
        self.port = 12345
        self.backlog = 10
        self.bufsize = 1024
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((, self.port))

    def run(self):
        print " === sub thread === "
        conn, address = self.sock.accept()
        while True:
            mes = conn.recv(self.bufsize)
            if mes == 'q':
                print "sub thread is being terminaited"
            print mes

if __name__ == '__main__':
    th = TestThread()

    # time.sleep(100)  #This is too long (command to wait 100 seconds)

    print " === main thread === "
    ip = "localhost"
    port = 12345
    bufsize = 1024
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    while True:
        inp = raw_input("YOU>")
        if inp == 'q':
            print "main thread is being terminated"

Recommended Posts

Socket communication and multi-thread processing by Python
Communication processing by Python
Socket communication by C language and Python
Python3 socket module and socket communication flow
Socket communication with Python
Send data from Python to Processing via socket communication
Image processing by python (Pillow)
Socket communication with Python LEGO Mindstorms
Python parallel processing (multiprocessing and Joblib)
python string processing map and lambda
100 Language Processing Knock Chapter 1 by Python
Image processing by Python 100 knock # 1 channel replacement
Socket communication using socketserver with python now
Grayscale by matrix-Reinventor of Python image processing-
100 image processing by Python Knock # 6 Color reduction processing
I tried to communicate with a remote server by Socket communication with Python.
python image processing
Communication and networking
Python file processing
Notify error and execution completion by LINE [Python]
Y / n processing in bash, python and Go
Image processing by Python 100 knock # 11 smoothing filter (average filter)
Python Socket communication sample / simple data throwing tool
Split Python images and arrange them side by side
Python> Sort by number and sort by alphabet> Use sorted ()
[Language processing 100 knocks 2020] Summary of answer examples by Python
Full-width and half-width processing of CSV data in Python
Notes on HDR and RAW image processing with Python
[Python] Measures and displays the time required for processing
Control other programs from Python (communication between Python and exe)
Python learning memo for machine learning by Chainer Chapters 1 and 2
Communication between uWSGI and Nginx using Unix Domain Socket
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
Python asynchronous processing ~ Full understanding of async and await ~
Interprocess communication between Ruby and Python (POSIX message queue)
Learn Python asynchronous processing / coroutines by comparing with Node.js
Summary of date processing in Python (datetime and dateutil)
HTTP server and HTTP client using Socket (+ web browser) --Python3
Serial communication with Python
Primality test by Python
Python distributed processing Spartan
Python and numpy tips
[Python] pip and wheel
Serial communication with python
File processing in Python
Python: Natural language processing
Visualization memo by Python
Multithreaded processing in python
Batch design and python
Python iterators and generators
First Python image processing
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Text processing in Python
Queue processing in Python
python input and output
Python and Ruby split
Python basics: Socket, Dnspython
WiringPi-SPI communication using Python
(Beginner) SSL socket communication