Package: hisock
hisock (HiSock, Hisock)Ahigher level extension of Python's built-insocket module, with simpler and more efficient usages.DocumentationDocumentation is located onReadTheDocs. Some documentation is also located in the code as docstrings, though most are
already well-documented on ReadTheDocs.InstallationHisock only supports Python versions 3.7 and onwards, due to annotations from the__future__module.hisockis available on PyPIhere, so it is installable bypip. Just do the following command.$python-mpipinstallhisock(WINDOWS)OR
$pip3installhisock(MAC/LINUX)To build thebleeding-edgeversion of hisock from GitHub, download the repository with your favorite way,cdinto it, then type:$python-mpipinstall-e.(WINDOWS)OR
$pip3install-e.(MAC/LINUX)Note that youMUSTcd into the cloned directory for this to work.Code of Conduct and Contributing Guidelines:Code of ConductContributing GuidelinesExampleshisock utilizes decorators as the core of receiving messages instead of having
if statements handling all of the logic.
More in-depth examples are located in theexamplesdirectory, including a tic-tac-toe game. Here is what a basic
server script would look like using hisock:Serverimportsysimporttimeimportrandomfromhisockimportstart_server,get_local_ipADDR=get_local_ip()PORT=6969iflen(sys.argv)==2:ADDR=sys.argv[1]iflen(sys.argv)==3:PORT=int(sys.argv[2])print(f"Serving at{ADDR}")server=start_server((ADDR,PORT))@server.on("join")defclient_join(client_data):print(f"Cool,{client_data.ip_as_str}joined!")ifclient_data['name']isnotNone:print(f"    - With a sick name\"{client_data.name}\", very cool!")ifclient_data['group']isnotNone:print(f"    - In a sick group\"{client_data.group}\", cool!")print("I'm gonna send them a quick hello message")server.send_client(client_data['ip'],"hello_message",str(time.time()).encode())@server.on("processing1")defprocess(client_data,process_request:str):print(f"\nAlright, looks like{client_data.ip_as_str}received the hello message, ""\nas now they're trying to compute something on the server, because they have ""potato computers")print("Their processing request is:",process_request)for_inrange(process_request.count("randnum")):randnum=str(random.randint(1,100000000))process_request=process_request.replace("randnum",randnum,1)result=eval(process_request)# Insecure, but I'm lazy, so...print(f"Cool! The result is{result}! I'mma send it to the client")server.send_client(client_data,"something",str(result))server.start()Clientimporttimefromhisockimportconnect,get_local_ipserver_to_connect=input("Enter server IP to connect to (Press enter for default of your local IP): ")port=input("Enter Server port number (Press enter for default of 6969): ")ifserver_to_connect=='':server_to_connect=get_local_ip()ifport=='':port=6969else:port=int(port)name=input("Name? (Press enter for no name) ")group=input("Group? (Press enter for no group) ")print("======================================= ESTABLISHING CONNECTION =======================================")ifname=='':name=Noneifgroup=='':group=Noneclient=connect((server_to_connect,port),name=name,group=group)join_time=time.time()@client.on("hello_message")defhandle_hello(msg:str):print("Thanks, server, for sending a hello, just for me!")print(f"Looks like, the message was sent on timestamp{msg}, "f"which is just{round(float(msg)-join_time,6)*1000}milliseconds since the connection!")print("In response, I'm going to send the server a request to do some processing")client.send("processing1",b"randnum**2")result=client.recv("something",int)print(f"WHOAAA! The result is{result}! Thanks server!")client.start()ContributorsSpecial thanks to the following:Sheepy0125, for refactoring the underhood code as well as the APISSS-Says-Snek, for maintaining and developing the libraryAnkith26, for helping me understand the usages of headersCopyright SSS-Says-Snek, 2021-present