How web servers use different ports to listen for requests and establish TCP connections

I"m going to write a simple web server with python to listen for requests on one port, and then process them on another port to establish a
TCP connection. Because the client sends the request with the destination port number, how can the server establish a TCP connection on another port?
such as

clientSocket.connect(xxxx, port1)

then the server:

connectionSocket, addr = serverSocket.accept()

has a connection been established on the port1 port? How do I establish a connection on the port2 port?

Nov.22,2021

the question is not clear: do you want to build a web server, a low-level "TCP server" or a high-level "HTTP server"?
seeing that the connect and accept methods are used in your code, I'll assume it's the first.
then, I feel that you are quite unclear about the concept of socket programming. The socket on the server side must be bound to a fixed address tuple (IP+ port). How can you change the port casually after binding?

post the single-threaded TCP server-client code I used when I was self-taught for your reference. If you don't understand, you can go through the "Socket Programming HOWTO" in the official manual first.

"""TCP:echo-server"""
import socket
import argparse

def server(host, port):
    """"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: -sharp
        sock.bind((host, port)) -sharp+
        sock.listen() -sharp
        print('listening at:', sock.getsockname())
        while True: -sharp
            conn, addr = sock.accept() -sharp
            with conn: -sharp
                print('connected by', addr)
                while True: -sharprecvallsend
                    data = conn.recv(1024) -sharp"" 1024
                    if not data: -sharpdata
                        break -sharp - conn
                    conn.sendall(data) -sharpdata
                    print('echoing', repr(data), 'to', addr)

def client(host, port):
    """"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect((host, port)) -sharp
        sock.sendall(b'simple is better than complex.') -sharp
        sock.shutdown(socket.SHUT_WR) -sharpsendrecvall
        while True: -sharprecvall
            data = sock.recv(1024) -sharp"" 1024
            print('received:', data)
            if not data: -sharpdata
                print('server socket closed.')
                break -sharp - sock

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='an echo-server using context manager')
    parser.add_argument('-n', metavar='hostname', default='127.0.0.1')
    parser.add_argument('-p', metavar='port', type=int, default=65432)
    parser.add_argument('-c', action='store_true', help='run as the client')
    args = parser.parse_args()
    run = client if args.c else server -sharp'-c'args.cNone
    run(args.n, args.p)
Menu