Created
January 9, 2023 11:21
-
-
Save Dbhardwaj99/464bab00234067490712820ce2e16248 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
# Create a socket object | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Get the local machine name | |
host = socket.gethostname() | |
# Set the port number | |
port = 12345 | |
# Bind the socket to the port | |
s.bind((host, port)) | |
# Start listening for incoming connections | |
s.listen(5) | |
# Wait for a connection | |
connection, address = s.accept() | |
print("Connected by", address) | |
# Receive data from the client | |
data = connection.recv(1024) | |
# Print the received data | |
print(data.decode()) | |
# Send a response to the client | |
message = "Hello, World!" | |
connection.sendall(message.encode()) | |
# Close the connection | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment