30 lines
546 B
Python
30 lines
546 B
Python
import socket
|
|
import urllib.parse
|
|
|
|
class Connection:
|
|
def __init__(self, uri):
|
|
parts = urllib.parse.urlparse(uri)
|
|
netloc = parts.netloc
|
|
self.host, _, self.port = netloc.partition(":")
|
|
|
|
|
|
def __enter__(self):
|
|
self.connect()
|
|
return self
|
|
|
|
def __exit__(self, exc_typ, exc_val, exc_tb):
|
|
pass
|
|
|
|
def connect(self):
|
|
self.socket = socket.socket()
|
|
self.socket.connect((self.host, int(self.port)))
|
|
|
|
def disconnect(self):
|
|
pass
|
|
|
|
def send(self, data):
|
|
self.socket.send(data)
|
|
|
|
def connection(uri) -> Connection:
|
|
return Connection(uri)
|