Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Reactor/Proactor patterns

import select
import socket


def main() -> None:
    host = socket.gethostname()
    port = 12345

    # create a TCP/IP socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.setblocking(0)
        # bind the socket to the port
        sock.bind((host, port))
        # listen for incoming connections
        sock.listen(5)
        print("Server started...")

        # sockets from which we expect to read
        inputs = [sock]
        outputs = []

        while inputs:
            # wait for at least one of the sockets to be ready for processing
            readable, writable, exceptional = select.select(inputs, outputs, inputs)

            for s in readable:
                if s is sock:
                    conn, addr = s.accept()
                    inputs.append(conn)
                else:
                    data = s.recv(1024)
                    if data:
                        print(data)
                    else:
                        inputs.remove(s)
                        s.close()

if __name__ == "__main__":
    main()
Comment

Reactor/Proactor patterns

import select
import socket


def main() -> None:
    host = socket.gethostname()
    port = 12345

    # create a TCP/IP socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.setblocking(0)
        # bind the socket to the port
        sock.bind((host, port))
        # listen for incoming connections
        sock.listen(5)
        print("Server started...")

        # sockets from which we expect to read
        inputs = [sock]
        outputs = []

        while inputs:
            # wait for at least one of the sockets to be ready for processing
            readable, writable, exceptional = select.select(inputs, outputs, inputs)

            for s in readable:
                if s is sock:
                    conn, addr = s.accept()
                    inputs.append(conn)
                else:
                    data = s.recv(1024)
                    if data:
                        print(data)
                    else:
                        inputs.remove(s)
                        s.close()

if __name__ == "__main__":
    main()
Comment

Reactor/Proactor patterns

import select
import socket


def main() -> None:
    host = socket.gethostname()
    port = 12345

    # create a TCP/IP socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.setblocking(0)
        # bind the socket to the port
        sock.bind((host, port))
        # listen for incoming connections
        sock.listen(5)
        print("Server started...")

        # sockets from which we expect to read
        inputs = [sock]
        outputs = []

        while inputs:
            # wait for at least one of the sockets to be ready for processing
            readable, writable, exceptional = select.select(inputs, outputs, inputs)

            for s in readable:
                if s is sock:
                    conn, addr = s.accept()
                    inputs.append(conn)
                else:
                    data = s.recv(1024)
                    if data:
                        print(data)
                    else:
                        inputs.remove(s)
                        s.close()

if __name__ == "__main__":
    main()
Comment

Reactor/Proactor patterns

import select
import socket


def main() -> None:
    host = socket.gethostname()
    port = 12345

    # create a TCP/IP socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.setblocking(0)
        # bind the socket to the port
        sock.bind((host, port))
        # listen for incoming connections
        sock.listen(5)
        print("Server started...")

        # sockets from which we expect to read
        inputs = [sock]
        outputs = []

        while inputs:
            # wait for at least one of the sockets to be ready for processing
            readable, writable, exceptional = select.select(inputs, outputs, inputs)

            for s in readable:
                if s is sock:
                    conn, addr = s.accept()
                    inputs.append(conn)
                else:
                    data = s.recv(1024)
                    if data:
                        print(data)
                    else:
                        inputs.remove(s)
                        s.close()

if __name__ == "__main__":
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: how to move mouse by detected face and eye using opencv 
Python :: vorticity 
Python :: pandas sample weights example 
Python :: python adding an item 
Python :: *9c0xxbz] 
Python :: get complete path from reletive path python 
Python :: dataframe ggplot rownames order 
Python :: python split respect quotes 
Python :: comments 
Python :: Python NumPy atleast_3d Function Example when inputs are high dimesion 
Python :: Python NumPy Shape function example Printing the shape of the multidimensional array 
Python :: text xml 
Python :: sensitivity 
Python :: Python NumPy asarray_chkfinite Function Example List to an array 
Python :: Python NumPy array_split Function Example 01 
Python :: Python NumPy repeat Function Example Working with 1D array 
Python :: max index tuple 
Python :: Python how to use __ne__ 
Python :: program adxl335 python 
Python :: NumPy bitwise_xor Code When inputs are Boolean 
Python :: ax bar different colors 
Python :: Python matplotlib multiple bars 
Python :: Remove Brackets from List Using join method 
Python :: make dialog in the front by Pywinauto 
Python :: python Tkinter widget displacement with pack() 
Python :: how to make a yes or no question in python 
Python :: how to plot graph between f1 score and random forest parameters 
Python :: pandas drop zeros from series 
Python :: check if id is present in elasticsearch using python 
Python :: ring Copy Lists 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =