Server Script
import socket
msg = "Meeting at 14h, Office 279"
#Local IP Data
host = "192.168.0.103"
port = 54321
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Preparing for Connection
s.bind((host, port))
print("Server Listening")
s.listen(5)
conn, addr = s.accept()
print("Connection From: ", addr)
print("Sending Message: ")
conn.sendall(msg.encode())
while True:
inp = input(">>")
conn.sendall(inp.encode())
-
import socket
- This line imports thesocket
module, which is a built-in module in Python that provides low-level network communication functions. -
host = "192.168.0.103"
- This line defines a string variable namedhost
that stores the IP address of the remote host. -
port = 54321
- This line defines an integer variable namedport
that stores the port number to use for the connection. -
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- This line creates a new socket object nameds
using thesocket
module. The first argument,socket.AF_INET
, specifies the address family to use (IPv4), and the second argument,socket.SOCK_STREAM
, specifies the socket type to use (TCP). -
s.connect((host, port))
- This line connects the sockets
to the remote host and port specified byhost
andport
. -
while True:
- This line starts an infinite loop. -
data_from_server = s.recv(1024)
- This line receives data from the remote host through the sockets
and stores it in the variabledata_from_server
. The argument1024
specifies the maximum amount of data to be received at once. -
print("received: ", data_from_server.decode())
- This line prints the received data to the console. Thedecode()
method is used to convert the bytes received into a string that can be printed.
The overall purpose of this script is to connect to a remote host at a specified IP address and port number, and then continuously receive data from the remote host and print it to the console.
Client Script
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())
- The script imports the
socket
module for networking functionality. - The script defines two variables
host
andport
that represent the remote IP address and port number to connect to. - A new
socket
object is created withsocket.socket(socket.AF_INET, socket.SOCK_STREAM)
whereAF_INET
indicates IPv4 addressing andSOCK_STREAM
indicates a TCP socket. - The
connect()
method is called on the socket object with thehost
andport
arguments to establish a connection to the server. - The script enters an infinite loop using
while True:
to continuously receive data from the server. - The
recv()
method of the socket object is called with a buffer size of 1024 bytes to receive data from the server. - The received data is decoded from bytes to string using the
decode()
method. - The received data is printed to the console using the
print()
function, along with the message "received: ".
No comments:
Post a Comment