Comprehensive networking capabilities including HTTP clients/servers, URL handling, email processing, and low-level socket programming.
Modern HTTP client functionality and URL parsing utilities.
import urllib.request
import urllib.parse
import urllib.error
import http.client
# URL request functions
def urlopen(url, data=None, timeout=None, cafile=None, capath=None,
cadefault: bool = False, context=None) -> http.client.HTTPResponse: ...
def urlretrieve(url: str, filename=None, reporthook=None, data=None) -> tuple: ...
def install_opener(opener) -> None: ...
def build_opener(*handlers): ...
# Request class
class Request:
def __init__(self, url: str, data=None, headers: dict = {},
origin_req_host=None, unverifiable: bool = False,
method=None) -> None: ...
@property
def full_url(self) -> str: ...
@property
def type(self) -> str: ...
@property
def host(self) -> str: ...
@property
def origin_req_host(self) -> str: ...
@property
def selector(self) -> str: ...
@property
def data(self): ...
@property
def unverifiable(self) -> bool: ...
@property
def method(self) -> str: ...
def get_method(self) -> str: ...
def add_header(self, key: str, val: str) -> None: ...
def add_data(self, data) -> None: ...
def get_header(self, header_name: str, default=None) -> str: ...
def remove_header(self, header_name: str) -> None: ...
# URL parsing functions
def urlparse(urlstring: str, scheme: str = '', allow_fragments: bool = True): ...
def parse_qs(qs: str, keep_blank_values: bool = False,
strict_parsing: bool = False, encoding: str = 'utf-8',
errors: str = 'replace', max_num_fields: int = None,
separator: str = '&') -> dict: ...
def parse_qsl(qs: str, keep_blank_values: bool = False,
strict_parsing: bool = False, encoding: str = 'utf-8',
errors: str = 'replace', max_num_fields: int = None,
separator: str = '&') -> list: ...
def urlunparse(parts) -> str: ...
def urlsplit(urlstring: str, scheme: str = '', allow_fragments: bool = True): ...
def urlunsplit(parts) -> str: ...
def urljoin(base: str, url: str, allow_fragments: bool = True) -> str: ...
def urldefrag(url: str) -> tuple: ...
def quote(string: str, safe: str = '/', encoding=None, errors=None) -> str: ...
def quote_plus(string: str, safe: str = '', encoding=None, errors=None) -> str: ...
def unquote(string: str, encoding: str = 'utf-8', errors: str = 'replace') -> str: ...
def unquote_plus(string: str, encoding: str = 'utf-8', errors: str = 'replace') -> str: ...
# URL components
class ParseResult:
scheme: str
netloc: str
path: str
params: str
query: str
fragment: str
def geturl(self) -> str: ...
class SplitResult:
scheme: str
netloc: str
path: str
query: str
fragment: str
def geturl(self) -> str: ...Low-level HTTP client for making HTTP requests.
import http.client
class HTTPConnection:
def __init__(self, host: str, port=None, timeout=None,
source_address=None, blocksize: int = 8192) -> None: ...
def connect(self) -> None: ...
def close() -> None: ...
def request(self, method: str, url: str, body=None, headers: dict = {},
encode_chunked: bool = False) -> None: ...
def getresponse(self) -> HTTPResponse: ...
def set_debuglevel(self, level: int) -> None: ...
# HTTP methods
def get(self, url: str, headers: dict = {}) -> None: ...
def post(self, url: str, body=None, headers: dict = {}) -> None: ...
def put(self, url: str, body=None, headers: dict = {}) -> None: ...
def delete(self, url: str, headers: dict = {}) -> None: ...
def head(self, url: str, headers: dict = {}) -> None: ...
def options(self, url: str, headers: dict = {}) -> None: ...
class HTTPSConnection(HTTPConnection):
def __init__(self, host: str, port=None, key_file=None, cert_file=None,
timeout=None, source_address=None, context=None,
check_hostname=None, blocksize: int = 8192) -> None: ...
class HTTPResponse:
def read(self, amt=None) -> bytes: ...
def readinto(self, b: bytes) -> int: ...
def getheader(self, name: str, default=None) -> str: ...
def getheaders(self) -> list: ...
def fileno(self) -> int: ...
def isclosed(self) -> bool: ...
def info(self): ...
def geturl(self) -> str: ...
def getcode(self) -> int: ...
# Properties
@property
def status(self) -> int: ...
@property
def reason(self) -> str: ...
@property
def version(self) -> int: ...
@property
def msg(self): ...
@property
def headers(self): ...
# HTTP status constants
HTTP_CONTINUE: int = 100
HTTP_OK: int = 200
HTTP_CREATED: int = 201
HTTP_ACCEPTED: int = 202
HTTP_NO_CONTENT: int = 204
HTTP_MOVED_PERMANENTLY: int = 301
HTTP_FOUND: int = 302
HTTP_NOT_MODIFIED: int = 304
HTTP_BAD_REQUEST: int = 400
HTTP_UNAUTHORIZED: int = 401
HTTP_FORBIDDEN: int = 403
HTTP_NOT_FOUND: int = 404
HTTP_METHOD_NOT_ALLOWED: int = 405
HTTP_INTERNAL_SERVER_ERROR: int = 500
HTTP_NOT_IMPLEMENTED: int = 501
HTTP_BAD_GATEWAY: int = 502
HTTP_SERVICE_UNAVAILABLE: int = 503
# Exceptions
class HTTPException(Exception): ...
class NotConnected(HTTPException): ...
class InvalidURL(HTTPException): ...
class UnknownProtocol(HTTPException): ...
class UnknownTransferEncoding(HTTPException): ...
class UnimplementedFileMode(HTTPException): ...
class IncompleteRead(HTTPException): ...
class ImproperConnectionState(HTTPException): ...
class CannotSendRequest(ImproperConnectionState): ...
class CannotSendHeader(ImproperConnectionState): ...
class ResponseNotReady(ImproperConnectionState): ...
class BadStatusLine(HTTPException): ...
class LineTooLong(HTTPException): ...
class RemoteDisconnected(ConnectionResetError, BadStatusLine): ...Low-level network interface for creating network connections.
import socket
# Socket creation
def socket(family: int = AF_INET, type: int = SOCK_STREAM, proto: int = 0,
fileno=None) -> socket: ...
def socketpair(family=None, type: int = SOCK_STREAM, proto: int = 0) -> tuple: ...
def create_connection(address: tuple, timeout=None, source_address=None) -> socket: ...
def create_server(address: tuple, family: int = AF_INET, backlog=None,
reuse_port: bool = False, dualstack_ipv6: bool = False) -> socket: ...
# Socket class
class socket:
def __init__(self, family: int = AF_INET, type: int = SOCK_STREAM,
proto: int = 0, fileno=None) -> None: ...
# Connection methods
def bind(self, address: tuple) -> None: ...
def listen(self, backlog: int = None) -> None: ...
def accept(self) -> tuple: ... # Returns (socket, address)
def connect(self, address: tuple) -> None: ...
def connect_ex(self, address: tuple) -> int: ...
# Data transfer methods
def send(self, data: bytes, flags: int = 0) -> int: ...
def sendall(self, data: bytes, flags: int = 0) -> None: ...
def recv(self, bufsize: int, flags: int = 0) -> bytes: ...
def recvfrom(self, bufsize: int, flags: int = 0) -> tuple: ...
def sendto(self, data: bytes, address: tuple) -> int: ...
def sendto(self, data: bytes, flags: int, address: tuple) -> int: ...
# Socket control
def close() -> None: ...
def shutdown(self, how: int) -> None: ...
def fileno(self) -> int: ...
def setblocking(self, flag: bool) -> None: ...
def settimeout(self, value: float) -> None: ...
def gettimeout(self) -> float: ...
# Socket options
def setsockopt(self, level: int, optname: int, value) -> None: ...
def getsockopt(self, level: int, optname: int, buflen: int = None): ...
# Socket information
def getsockname(self) -> tuple: ...
def getpeername(self) -> tuple: ...
# Address resolution
def gethostname() -> str: ...
def gethostbyname(hostname: str) -> str: ...
def gethostbyaddr(ip_address: str) -> tuple: ...
def getfqdn(name: str = '') -> str: ...
def getaddrinfo(host: str, port, family: int = 0, type: int = 0,
proto: int = 0, flags: int = 0) -> list: ...
def getnameinfo(sockaddr: tuple, flags: int) -> tuple: ...
# Socket families
AF_UNIX: int # Unix domain sockets
AF_INET: int # IPv4
AF_INET6: int # IPv6
# Socket types
SOCK_STREAM: int # TCP
SOCK_DGRAM: int # UDP
SOCK_RAW: int # Raw sockets
# Protocol constants
IPPROTO_TCP: int
IPPROTO_UDP: int
IPPROTO_ICMP: int
# Socket options
SO_REUSEADDR: int
SO_KEEPALIVE: int
SO_BROADCAST: int
TCP_NODELAY: int
# Shutdown options
SHUT_RD: int # Shutdown reading
SHUT_WR: int # Shutdown writing
SHUT_RDWR: int # Shutdown both
# Exceptions
class error(OSError): ...
class herror(error): ...
class gaierror(error): ...
class timeout(error): ...Secure socket layer support for encrypted connections.
import ssl
# SSL context creation
def create_default_context(purpose: Purpose = Purpose.SERVER_AUTH,
cafile=None, capath=None, cadata=None) -> SSLContext: ...
def create_unverified_context(protocol=None, cert_reqs=None, check_hostname=None,
purpose: Purpose = Purpose.SERVER_AUTH,
certfile=None, keyfile=None, cafile=None,
capath=None, cadata=None) -> SSLContext: ...
# SSL wrapping
def wrap_socket(sock, keyfile=None, certfile=None, server_side: bool = False,
cert_reqs: int = CERT_NONE, ssl_version=None, ca_certs=None,
do_handshake_on_connect: bool = True,
suppress_ragged_eofs: bool = True, ciphers=None) -> SSLSocket: ...
# SSL context class
class SSLContext:
def __init__(self, protocol=None) -> None: ...
# Configuration
def load_cert_chain(self, certfile: str, keyfile=None, password=None) -> None: ...
def load_verify_locations(self, cafile=None, capath=None, cadata=None) -> None: ...
def load_default_certs(self, purpose: Purpose = Purpose.SERVER_AUTH) -> None: ...
def set_ciphers(self, ciphers: str) -> None: ...
def set_alpn_protocols(self, protocols: list) -> None: ...
# Socket wrapping
def wrap_socket(self, sock, server_side: bool = False,
do_handshake_on_connect: bool = True,
suppress_ragged_eofs: bool = True,
server_hostname=None, session=None) -> SSLSocket: ...
def wrap_bio(self, incoming, outgoing, server_side: bool = False,
server_hostname=None, session=None) -> SSLObject: ...
# Properties
@property
def protocol(self): ...
@property
def verify_mode(self) -> int: ...
@verify_mode.setter
def verify_mode(self, value: int) -> None: ...
@property
def check_hostname(self) -> bool: ...
@check_hostname.setter
def check_hostname(self, value: bool) -> None: ...
# SSL socket class
class SSLSocket(socket.socket):
def do_handshake(self, block: bool = False) -> None: ...
def getpeercert(self, binary_form: bool = False) -> dict: ...
def cipher(self) -> tuple: ...
def compression(self) -> str: ...
def get_channel_binding(self, cb_type: str = 'tls-unique') -> bytes: ...
def selected_alpn_protocol(self) -> str: ...
def selected_npn_protocol(self) -> str: ...
def unwrap(self) -> socket.socket: ...
def version(self) -> str: ...
def pending(self) -> int: ...
# Socket methods with SSL
def read(self, len: int = 1024, buffer=None) -> bytes: ...
def write(self, data: bytes) -> int: ...
# Certificate verification modes
CERT_NONE: int # No certificate verification
CERT_OPTIONAL: int # Certificate verification optional
CERT_REQUIRED: int # Certificate verification required
# SSL versions
PROTOCOL_SSLv23: int # Negotiate highest protocol version
PROTOCOL_TLS: int # Alias for PROTOCOL_SSLv23
PROTOCOL_TLSv1: int # TLS 1.0
PROTOCOL_TLSv1_1: int # TLS 1.1
PROTOCOL_TLSv1_2: int # TLS 1.2
# Purposes
class Purpose:
SERVER_AUTH: Purpose # Server authentication
CLIENT_AUTH: Purpose # Client authentication
# Exceptions
class SSLError(OSError): ...
class SSLZeroReturnError(SSLError): ...
class SSLWantReadError(SSLError): ...
class SSLWantWriteError(SSLError): ...
class SSLSyscallError(SSLError): ...
class SSLEOFError(SSLError): ...
class SSLCertVerificationError(SSLError): ...
class CertificateError(ValueError): ...Comprehensive email message parsing, construction, and handling.
import email
import email.message
import email.mime.text
import email.mime.multipart
# Message parsing
def message_from_string(s: str, _class=None, policy=None) -> EmailMessage: ...
def message_from_bytes(s: bytes, _class=None, policy=None) -> EmailMessage: ...
def message_from_file(fp, _class=None, policy=None) -> EmailMessage: ...
def message_from_binary_file(fp, _class=None, policy=None) -> EmailMessage: ...
# Email message class
class EmailMessage:
def __init__(self, policy=None) -> None: ...
# Header access
def __getitem__(self, name: str) -> str: ...
def __setitem__(self, name: str, val: str) -> None: ...
def __delitem__(self, name: str) -> None: ...
def __contains__(self, name: str) -> bool: ...
def keys(self) -> list: ...
def values(self) -> list: ...
def items(self) -> list: ...
def get(self, name: str, failobj=None) -> str: ...
def get_all(self, name: str, failobj=None) -> list: ...
# Message construction
def add_header(self, _name: str, _value: str, **_params) -> None: ...
def replace_header(self, _name: str, _value: str) -> None: ...
def set_content(self, content, maintype=None, subtype=None,
charset=None, cte=None, disposition=None,
filename=None, cid=None, params=None, headers=None) -> None: ...
def add_related(self, *args, **kw) -> None: ...
def add_alternative(self, *args, **kw) -> None: ...
def add_attachment(self, *args, **kw) -> None: ...
# Message properties
def is_multipart(self) -> bool: ...
def get_content_type(self) -> str: ...
def get_content_maintype(self) -> str: ...
def get_content_subtype(self) -> str: ...
def get_default_type(self) -> str: ...
def set_default_type(self, ctype: str) -> None: ...
def get_params(self, failobj=None, header: str = 'content-type',
unquote: bool = True) -> list: ...
def get_param(self, param: str, failobj=None, header: str = 'content-type',
unquote: bool = True) -> str: ...
def set_param(self, param: str, value: str, header: str = 'Content-Type',
requote: bool = True, charset=None, language: str = '',
replace: bool = False) -> None: ...
def del_param(self, param: str, header: str = 'content-type',
requote: bool = True) -> None: ...
# Content access
def get_content(self) -> object: ...
def get_body(self, preferencelist=('related', 'html', 'plain')) -> EmailMessage: ...
def iter_attachments(self): ...
def iter_parts(self): ...
# Message serialization
def as_string(self, unixfrom: bool = False, maxheaderlen: int = 0,
policy=None) -> str: ...
def as_bytes(self, unixfrom: bool = False, policy=None) -> bytes: ...
def __str__(self) -> str: ...
def __bytes__(self) -> bytes: ...
# MIME message types
class MIMEText:
def __init__(self, _text: str, _subtype: str = 'plain', _charset=None) -> None: ...
class MIMEMultipart:
def __init__(self, _subtype: str = 'mixed', boundary=None, _subparts=None,
**_params) -> None: ...
def attach(self, payload) -> None: ...
class MIMEBase:
def __init__(self, _maintype: str, _subtype: str, **_params) -> None: ...
def add_header(self, _name: str, _value: str, **_params) -> None: ...
class MIMEApplication(MIMEBase):
def __init__(self, _data: bytes, _subtype: str = 'octet-stream',
_encoder=None, **_params) -> None: ...
class MIMEImage(MIMEBase):
def __init__(self, _imagedata: bytes, _subtype=None, _encoder=None,
**_params) -> None: ...
class MIMEAudio(MIMEBase):
def __init__(self, _audiodata: bytes, _subtype=None, _encoder=None,
**_params) -> None: ...# URL parsing result types
class URLParseResult:
scheme: str # URL scheme (http, https, ftp, etc.)
netloc: str # Network location (hostname:port)
path: str # Path component
params: str # Parameters
query: str # Query string
fragment: str # Fragment identifier
class URLSplitResult:
scheme: str # URL scheme
netloc: str # Network location
path: str # Path component
query: str # Query string
fragment: str # Fragment identifier# Socket address types
SocketAddress = tuple[str, int] # IPv4 address (host, port)
IPv6Address = tuple[str, int, int, int] # IPv6 address (host, port, flow_info, scope_id)
UnixAddress = str # Unix socket path
# Address info structure
class AddressInfo:
family: int # Address family (AF_INET, AF_INET6)
type: int # Socket type (SOCK_STREAM, SOCK_DGRAM)
proto: int # Protocol number
canonname: str # Canonical name
sockaddr: tuple # Socket address# Certificate information
class CertificateInfo:
"""SSL certificate information dictionary."""
subject: tuple # Certificate subject
issuer: tuple # Certificate issuer
version: int # Certificate version
serialNumber: str # Serial number
notBefore: str # Valid from date
notAfter: str # Valid until date
subjectAltName: tuple # Subject alternative names
# SSL cipher information
SSLCipher = tuple[str, str, int] # (cipher_name, ssl_version, secret_bits)# HTTP headers type
HTTPHeaders = dict[str, str]
# HTTP message info
class HTTPMessage:
"""HTTP message headers container."""
def __getitem__(self, name: str) -> str: ...
def __contains__(self, name: str) -> bool: ...
def keys(self) -> list: ...
def values(self) -> list: ...
def items(self) -> list: ...