import socket
#Remote IP and port
host = "192.168.0.103"
port = 54321
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
#Data from server
while True:
data_from_server = s.recv(1024)
print("received: ", data_from_server.decode())
This script uses the socket
module in Python to establish a connection with a server and receive data from it. The socket
module provides a low-level interface to network communication, allowing developers to send and receive data over various protocols, such as TCP, UDP, and others.
The first step is to define the remote IP and port to which the client will connect. In this case, the IP address is 192.168.0.103
, and the port is 54321
.
The script then creates a new socket using the socket.socket
method and sets its address family to AF_INET
(IPv4) and the socket type to SOCK_STREAM
(TCP).
Next, the script establishes a connection to the remote server using the connect
method of the socket object. This method takes as input a tuple containing the IP address and port number of the server.
After the connection is established, the client enters a loop where it continuously receives data from the server using the recv
method of the socket object. The recv
method takes as input the maximum number of bytes to be received at once.
Once the data is received, the script decodes it from bytes to a string using the decode
method, and then prints it to the console using the print
function.
The loop continues until the connection is closed, or an error occurs, at which point the script exits.
No comments:
Post a Comment