The socket abstraction
socket
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)connect
host_port = ("127.0.0.1", 4321)
sock.connect(host_port)send
string_bytes = "Sockets are great!".encode("utf-8")
bytes_len = len(string_bytes)
num_bytes_to_send = bytes_len
while num_bytes_to_send > 0:
# Sometimes, the operating system cannot send everything immediately.
# For example, the sending buffer may be full.
# send returns the number of bytes that were sent.
num_bytes_to_send -= sock.send(string_bytes[bytes_len-num_bytes_to_send:])recv
Exceptions
Last updated