-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5. TCP IP model.py
25 lines (18 loc) · 1.09 KB
/
5. TCP IP model.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
# The TCP/IP model is a conceptual model, not a specific protocol or code. However, here is an example of Python code that uses the TCP/IP protocol stack to establish a connection between two devices using the socket module:
import socket
# Define the IP address and port number of the server
SERVER_IP = '127.0.0.1'
SERVER_PORT = 12345
# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
client_socket.connect((SERVER_IP, SERVER_PORT))
# Send a message to the server
message = 'Hello, server!'
client_socket.send(message.encode())
# Receive a response from the server
response = client_socket.recv(1024)
print(response.decode())
# Close the connection
client_socket.close()
# In this example, a client socket is created and connected to a server socket using the TCP/IP protocol stack. A message is sent to the server and a response is received, and then the connection is closed. This is a simple example, but it demonstrates the basics of using the TCP/IP protocol stack in Python to establish a connection and exchange data between devices.