NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#!/usr/bin/python3

import socket

class SocketCon:
def __init__(self, url: str, port: int, username: str, password: str = None ):
self.url = url
self.port = port
self.username = username
self.password = password

def make_con(self) -> bool:
try:
ip_address = socket.gethostbyname(self.url)
print(f"Resolved {self.url} to ip-address {ip_address}")

# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(5)

sock.connect((ip_address, self.port))
print(f"Successful connection to: {ip_address}:{self.port}")

# Handle server response
self.handle_response(sock)
return True

except socket.timeout:
print(f"Connection timeout!")
return False

except socket.error as e:
print(f"Socket error: {e}")
return False

def handle_response(self, sock):
"""Handle the response from the server."""
try:
if self.password:
self.send_command(sock, f"PASS {self.password}")
# Send IRC NICK and USER commands
self.send_command(sock, f"NICK {self.username}")
self.send_command(sock, f"USER {self.username} 0 * :{self.username}")

# Continuously read responses from the server
while True:
response = sock.recv(4096)
if not response:
break

response_decoded = response.decode('utf-8', errors='ignore')
print("Received response from the server:")
print(response_decoded)

# Handle PING messages
if response_decoded.startswith("PING"):
self.handle_ping(sock, response_decoded)

# Implement custom handling logic here
self.custom_handler(response_decoded)

except socket.error as e:
print(f"Error receiving data: {e}")

def send_command(self, sock, command: str):
"""Send a command to the IRC server."""
try:
command_with_newline = command + "rn"
sock.send(command_with_newline.encode('utf-8'))
print(f"Sent command: {command}")
except socket.error as e:
print(f"Error sending command: {e}")

def handle_ping(self, sock, message: str):
"""Respond to PING messages from the server."""
try:
ping_response = message.replace("PING", "PONG")
self.send_command(sock, ping_response)
except socket.error as e:
print(f"Error handling PING: {e}")

def custom_handler(self, response: str):
"""Custom handler for server responses."""
# Example: Print the response length and content type
print(f"Response length: {len(response)} characters")
if "Content-Type: " in response:
start = response.find("Content-Type: ") + len("Content-Type: ")
end = response.find("rn", start)
content_type = response[start:end]
print(f"Content-Type: {content_type}")


if __name__ == "__main__":
url = "localhost"
port = 6667
password = "asdfqwer123"
username = "demo123"

conn = SocketCon(url, port, username, password)

if conn.make_con():
print("Connection successful!")
else:
print("Connection failed!")
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.