AF_INET
127.0.0.1
) or in its 32-bit form
(0x7F000001'
)AF_INET6
127.0.0.1
is represented as
0000:0000:0000:0000:0000:ffff:7f00:0001
AF_UNIX
SOCK_DGRAM
SOCK_STREAM
socket
import socket
socket()
from the modulesocket()
function is the following:x = socket.socket(socket_family, socket_type, protocol=0)
family
: refers to the address
familytype
: refers to the type of the
socketproto
: this number is usually zero; but
can be different in other use-casessocket
Module isocket.recv(buflen)
sockwt.recfrom(buflen)
socket.rec_into(buffer)
socket.recfrom_into(buffer)
socket.send(bytes)
socket
Module iisocket.sendto(data, address)
socket.sendall(data)
socket.close()
socket
module?socket
Module iiiip
and
port
with the values of the local
address/site you wish to connect to
ip = 127.0.0.1
and
port = 80
AF_INET
- the type of address family
used, in this case IPv4SOCK_STREAM
- the type of connection
being made, in this case enabling us to send and receive
a message to a web serverconnect
function as
a tuple arguments.connect( (ip, port) )
sendall
function
s.sendall("GET /5062CEM.php\r\n")
recv
function
recv
accepts a single argument, and it
is the number of bytes to retrieve from the web
servers.recv(1024)
import socket
ip = '127.0.0.1'
port = 80
message = "GET /5062CEM.php\r\n"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, port))
s.sendall(message.encode())
data = s.recv(1024)
data -> b’<html>\n<head>\n\t<title>5062CEM - Networking Demo</title>\n</head>\n<body>\nHello, welcome to 5062CEM!</body>\n’
data2 = s.recv(10)
data2 -> b’<html>\n<he’
socket
Module ivip
and
port
with the values of the local
address/site you wish to connect to
ip = 127.0.0.1
and
port = 80
AF_INET
- the type of address family
used, in this case IPv4SOCK_STREAM
- the type of connection
being made, in this case enabling us to send and receive
a message to a web serverconnect
function as
a tuple arguments.connect( (ip, port) )
sendall
function
s.sendall("GET /5062CEM.php?lecturer=Ian%20Cornelius \r\n")
recv
function
recv
accepts a single argument, and it
is the number of bytes to retrieve from the web
servers.recv(136)
import socket
ip = '127.0.0.1'
port = 80
message = "GET /5062CEM.php?lecturer=Ian%20Cornelius \r\n"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, port))
s.sendall(message.encode())
data = s.recv(136)
data -> b’<html>\n<head>\n\t<title>5062CEM - Networking Demo</title>\n</head>\n<body>\nHello Ian Cornelius, and welcome to 5062CEM!</body>\n’
message = "GET /5062CEM.php?lecturer=Terry%20Richards \r\n"
data -> b’<html>\n<head>\n\t<title>5062CEM - Networking Demo</title>\n</head>\n<body>\nHello Terry Richards, and welcome to 5062CEM!</body>\n’
socket
Functionssocket.bind(address)
socket.listen(count)
socket.accept()
bind()
and listen()
should
be called before using this functionsocket
Functionssocket.connect(address
)
socket.connect_ex(address)
connect()