SoFunction
Updated on 2024-11-17

python create tcp server and client using socket

python uses sockets to create tcp servers and clients.

The server side is a timestamp server that automatically replies after receiving data from the client.

Client, waits for user input, returns and sends the user input to the server.

Tested under python2.7 and python3.6 respectively. You need to start the server side first and the client side at startup.

under python 2.7

The server-side code is

#coding:utf-8

from socket import *
from time import ctime

print("=====================timestampTCPserver (computer)=====================");

HOST = '' # The host number is blank to indicate that any available address can be used.
PORT = 21567 # port number
BUFSIZ = 1024 #Receive data buffer size
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create TCP server sockets
(ADDR) # Sockets bound to addresses
(5) # Listen for connections, maximum number of simultaneous connection requests

while True:
  print('Waiting for the client to connect...')
  tcpCliSock, addr = ()  # Receive client connection requests
  print('Get Connection:', addr)

  while True:
    data = (BUFSIZ) # Receive the specified bytes of data consecutively, received as a byte array
    if not data: #If the data is blank, the client exits, so exit reception
      break
    ('[%s] %s' % (ctime(), data)) # Send timestamp data to the client

  () # Close the connection with the client
() # Close the server socket

The client code is

#coding:utf-8

from socket import *

print("=====================TCPclient (computing)=====================");

HOST = '127.0.0.1' # Server ip address, equivalent to localhost.
PORT = 21567 # Communication port number
BUFSIZ = 1024 #Receive data buffer size
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM) # Create client sockets
(ADDR) # Initiate a TCP connection

while True:
  data = raw_input('> ')  # Receive user input
  if not data: #If the user input is empty, just enter will send "", "" is for false
    break
  (data)  # Clients send messages
  data = (BUFSIZ)  #Receive response messages, received as byte arrays
  if not data:  # If receiving a server message fails, or there is no message response
    break
  print(data) #Print Response Message

() # Close the client socket

under python 3.6

The server-side code is

#coding:utf-8

from socket import *
from time import ctime

print("=====================timestampTCPserver (computer)=====================");

HOST = '127.0.0.1' # The host number is blank to indicate that any available address can be used.
PORT = 21567 # port number
BUFSIZ = 1024 #Receive data buffer size
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create TCP server sockets
(ADDR) # Sockets bound to addresses
(5) # Listen for connections, maximum number of simultaneous connection requests

while True:
  print('Waiting for the client to connect...')
  tcpCliSock, addr = ()  # Receive client connection requests
  print('Get Connection:', addr)

  while True:
    data = (BUFSIZ) # Receive the specified bytes of data consecutively, received as a byte array
    if not data: #If the data is blank, the client exits, so exit reception
      break
    #('[%s] %s' % (bytes(ctime(), 'utf-8'), data))
    (bytes('[%s] %s' % (ctime(), ('utf-8')), 'utf-8')) # Send timestamp data to client, must send byte array

  () # Close the connection with the client
() # Close the server socket

The client code is

#coding:utf-8

from socket import *

print("=====================TCPclient (computing)=====================");

HOST = '127.0.0.1' # Server ip address, equivalent to localhost.
PORT = 21567 # Communication port number
BUFSIZ = 1024 #Receive data buffer size
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM) # Create client sockets
(ADDR) # Initiate a TCP connection

while True:
  data = input('> ')  # Receive user input
  if not data: #If the user input is empty, just enter will send "", "" is for false
    break
  (bytes(data, 'utf-8'))  # Client sends message, must send byte array
  data = (BUFSIZ)  #Receive a response message, received as an array of bytes
  if not data:  # If receiving a server message fails, or there is no message response
    break
  print(('utf-8')) # Print the response message, or str(data, "utf-8")

() # Close the client socket

This is the whole content of this article.