-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. OSI Model Open System Interconnection.py
59 lines (42 loc) · 1.93 KB
/
4. OSI Model Open System Interconnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# The OSI model is a conceptual model and does not have a specific implementation or programming interface. However, network protocols and applications can be designed and implemented using the OSI model as a reference.
#Here is an example of Python code that implements a simple client-server network communication using the TCP/IP protocols, which are based on the OSI model:
#Server code:
import socket
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific IP address and port number
server_address = ('localhost', 8000)
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
print('Server is listening on', server_address)
while True:
# Wait for a client to connect
print('Waiting for a client to connect...')
client_socket, client_address = server_socket.accept()
print('Client', client_address, 'connected!')
# Receive data from the client
data = client_socket.recv(1024)
print('Received data:', data.decode())
# Send a response to the client
message = 'Hello from the server!'
client_socket.sendall(message.encode())
# Close the client socket
client_socket.close()
# Client code:
import socket
# Create a TCP/IP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
server_address = ('localhost', 8000)
client_socket.connect(server_address)
print('Connected to', server_address)
# Send a message to the server
message = 'Hello from the client!'
client_socket.sendall(message.encode())
# Receive a response from the server
data = client_socket.recv(1024)
print('Received data:', data.decode())
# Close the socket
client_socket.close()
# This code demonstrates how the TCP/IP protocols, which are based on the OSI model, are used to establish a connection between a client and a server, transfer data between them, and then close the connection.