Skip to content

Instantly share code, notes, and snippets.

@Dbhardwaj99
Created January 9, 2023 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dbhardwaj99/464bab00234067490712820ce2e16248 to your computer and use it in GitHub Desktop.
Save Dbhardwaj99/464bab00234067490712820ce2e16248 to your computer and use it in GitHub Desktop.
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