How to create a simple server using socket module in python?

Description

To create a simple server using socket programming in python3.

  • Import socket library.
  • Create socket object s.
  • Define the port number
    (5 digits).
  • Bind the port and socket
    object using s.bind().
  • Check socket has been created
    or not.
  • Server listening if any connection
    from client using s.listen().
  • If connection available accept
    the client using s.accept().
  • Then stop the server.

#import socket module
import socket

#create socket object
s = socket.socket()
print(“Socket created successfully”)
port = 54546
#Bind the port
s.bind((”, port))

#check the socket binded to the port
print(“socket binded to “,(port))
s.listen(5)
print(“server listening”)
while True:

#accept the connection
c, addr=(s.accept())

#send status message to the client
c.send(‘connected’)

#Terminate the connection
c.close()

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit