Skip to content

A library to build off later, to make socket programming faster in python

License

Notifications You must be signed in to change notification settings

icefisher225/py_sockets

Repository files navigation

py_sockets

This is a library to make error-handled socket programming faster and easier.

Written for Python 3.9.x.

Dependencies

Builtins:

  • socket
  • queue

Packages:

  • strictly (possibly to be removed in future update)
  • caoe (thread orphanage prevention)
python3 -m pip install strictly
python3 -m pip install caoe

Helpful modules for using this library:

  • multiprocessing (server-side only)
  • time
  • threading

Sample Code

Server:

import multiprocessing as mp #used to allow multiple concurrent client connections.
from server_socketlib import ServerSocket, ServerCommSocket, ExitCode, get_connection

def run(commsock, queue):
    commsock.send(f"host attached = {commsock.ip}")
    check = (data := commsock.recv()).split(" ")[0]
    if check == "exit" or check == "quit":
        queue.put(ExitCode())
    print(data)
    return 0


def main():
    ip = "localhost"
    port = 12345
    process_list = list()
    comqueue = mp.Queue()
    # This is used for communication back to main loop ONLY

    server_socket = ServerSocket(ip, port, comqueue)    

    while True:
        communication_socket = get_connection(server_socket, num=15, timeout=60)
        if communication_socket.error == None:
            clientprocess = mp.Process(target = run, args(commsock, comqueue, ))
            clientprocess.start()
            process_list.append(clientprocess)
        else:
            print(f"error: {communication_socket.msg}")
            break
            # If this "break" is removed, the serversocket can never be told
            # to exit from the client and will need to be exited locally.

    for process in process_list:
        wait(process)
    return 0

if __name__ == __"main__":
    main()

Client:

from client_socketlib import ClientSocket
import time

ip = "10.0.1.200"
port = 12345

sock = ClientSocket(ip, port)

if sock.connect() == 1:
    print(sock.error())
    return 1

sock.send("Hello, World!")

time.sleep(2)

print(sock.recv())

About

A library to build off later, to make socket programming faster in python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages