Skip to content

silicWulf/shoes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

shoes - because socks weren't enough

Shoes is a module for Python 3 that makes sockets easier for users, while retaining the normal sockets function.

FAQ

What is shoes?

Look up like three lines mate!

Why should I use shoes?

You shouldn't, if you're experienced with sockets. This is a toned-down wrapper for sockets that is meant for new programmers. It provides a far easier interface for creating socket servers and clients.

An example of this being easier: let's say we wanted to make a server that sent a message "Hello!" to all the connected users every second. We need notice if the user is disconnected as well. Here's the code in the regular socket module:

import time
import socket
import threading
sock=socket.socket()
sock.bind("0.0.0.0",11111)
sock.listen(0)
conns=[]
def listener():
	global conns
	while True:
		conn,addr=sock.accept()
		conns.append(conn)
thread1=threading.Thread(target=listener)
thread1.daemon=True
thread1.start()
while 1:
	time.sleep(1)
	for conn in conns:
		try:
			conn.send(bytes("Hello!",encoding='utf-8'))
		except:
			print("User has disconnected!")

...and here's the code in the shoes module:

import time
import shoes
sock=shoes.Server("0.0.0.0",11111)
sock.listen()
while 1:
	time.sleep(1)
	sock.sendall("Hello!")

A pretty vast difference, huh? It takes up roughly 4x less space than vanilla socket.

If you are an experienced developer that doesn't really need the fancy/cool stuff from socket and just wants a minimalistic way to manage sockets then this module can support that want.

Documentation

shoes.Socket(ip,port)

A basic Socket class, used for connecting to servers.

  • ip -- (str) The IP address to connect to
  • port -- (int) The port to connect through

      send(message)

        Sends a message to the connected server.

         * message -- (str) The message to send to the server

      receive()

        Returns decoded data from the server. This is also aliased as "recieve" because I don't know how to spell.

      disconnect()

        Disconnects from the server.

      timeout(seconds)

        Sets the timeout for most socket functions to seconds seconds.

         * seconds -- (int) The number of seconds that the timeout should be set to. Can also be None to set no timeout.

shoes.Server(ip,port)

A basic Server class, used for starting a server. Note: the server must be listening in order to accept any connections.

  • ip -- (str) The IP address to bind the server to
  • port -- (int) The port to bind the server to

      send(conn,message)

        Sends a message to the connected client.

         * conn -- (socket.socket) The connection socket to the client to send the message to

         * message -- (str) The message to send to the client conn

      sendall(message)

        Sends a message to all connected clients.

         * message -- (str) The message to send to the client conn

      receive(conn)

        Returns received data from a connected client. This is also aliased as "recieve" because I don't know how to spell.

         * conn -- (socket.socket) The connection socket to the client to send the message to

      listen()

        Starts the listening of a server to accept connections.

        INFO: Once listening and connections have been made between the server and client, Server.user_count and Server.connections become non-zero/non-None.

      stoplisten()

        Stops the listening of a server to accept connections. Note: connections that have already been made will not be terminated.

      autorefresh()

        This function starts updating the user_count and connections as much as possible, not just when an attempt to send or receieve from a connection is made. NOTE: THIS PRODUCES ERRORS IF THE CLIENT IS NOT USING THIS MODULE! This is because in order to keep the connections updated, it needs to send a character that can cause errors with programs that listen and interpret every single thing sent to it.

      timeout(seconds)

        Sets the timeout for most socket functions to seconds seconds.

         * seconds -- (int) The number of seconds that the timeout should be set to. Can also be None to set no timeout.

      connections

         (list(tuple(socket.socket, tuple(str, int)))) (jeez that was a mouthful) A list of tuples containing a connection socket.socket and another tuple containing the IP address and port of the client.

      user_count

         (int) An int of how many users are connected.

About

Easy-to-use python3 sockets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages