A server that will receive a connection from a client, send a string to the client, and close the connection. : socket « Network « Python Tutorial






import socket

HOST = "127.0.0.1"
PORT = 5000
counter = 0

mySocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )

try:
   mySocket.bind( ( HOST, PORT ) ) 
except socket.error:
   print "Call to bind failed"

while 1:
   print "Waiting for connection"
   mySocket.listen( 1 )

   connection, address = mySocket.accept()
   counter += 1
   print "Connection", counter, "received from:", address[ 0 ]

   connection.send( "SERVER Connection successful" )
   clientMessage = connection.recv( 1024 )
   while clientMessage != "CLIENT TERMINATE":
      if not clientMessage:
         break
      print clientMessage
      serverMessage = "message" 
      connection.send( "SERVER " + serverMessage )
      clientMessage = connection.recv( 1024 )
   print "Connection terminated"
   connection.close()








21.2.socket
21.2.1.Get list of available socket options
21.2.2.Implementing Internet Communication
21.2.3.A server that will receive a connection from a client, send a string to the client, and close the connection.
21.2.4.socket.socket(socket.AF_INET, socket.SOCK_STREAM)
21.2.5.Looking up port number
21.2.6.Get socket name and peer name
21.2.7.Send 10M of data with socket
21.2.8.Echo Server