杰瑞科技汇

Python如何连接指定端口?

Of course! Connecting to a port in Python is a fundamental networking task. The most common and standard way to do this is using Python's built-in socket module.

Python如何连接指定端口?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide, starting from the basics and moving to more practical examples.

The Core Concept: Client-Server Model

When you "connect to a port," you are almost always acting as a client trying to reach a server that is listening on a specific port on a specific machine.

  • Server: A program that runs on a machine, waits for incoming connections on a specific port, and processes requests.
  • Client: A program that initiates a connection to a server's IP address and port.
  • Port: A number (1-65535) that acts as a door or endpoint for communication. Servers "listen" on a port, and clients "connect" to it.

Basic Client Connection using socket

This is the most direct way to connect to a port. We'll create a simple client that connects to an example server.

The Code

# client.py
import socket
# --- Configuration ---
# The IP address of the server you want to connect to.
# '127.0.0.1' is the loopback address, meaning "this same machine".
HOST = '127.0.0.1'  
# The port the server is listening on.
PORT = 65432       
print(f"Attempting to connect to {HOST}:{PORT}...")
try:
    # 1. Create a socket object
    # socket.AF_INET: Use IPv4
    # socket.SOCK_STREAM: Use TCP (reliable, connection-oriented)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # 2. Connect to the server
        s.connect((HOST, PORT))
        print("Successfully connected!")
        # 3. Send data to the server (must be in bytes)
        message = "Hello, Server!"
        print(f"Sending: {message}")
        s.sendall(message.encode('utf-8'))
        # 4. Receive data from the server (buffer size of 1024 bytes)
        data = s.recv(1024)
        # 5. Decode the received bytes and print
        print(f"Received from server: {data.decode('utf-8')}")
except ConnectionRefusedError:
    print(f"Connection failed! Is the server running on {HOST}:{PORT}?")
except socket.error as e:
    print(f"A socket error occurred: {e}")

How to Run This Example

You cannot run this client script by itself. You need a server running on the same machine (0.0.1) and port (65432) to accept the connection.

Python如何连接指定端口?-图2
(图片来源网络,侵删)

Here is a simple server script you can run in a separate terminal:

# server.py
import socket
HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)
print(f"Server starting on {HOST}:{PORT}...")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()  # Enable the server to accept connections
    print("Server is listening for a connection...")
    # .accept() waits for a client to connect and returns a new socket object 'conn'
    # to communicate with that client, and the address of the client.
    conn, addr = s.accept()
    with conn:
        print(f"Connected by {addr}")
        while True:
            data = conn.recv(1024)
            if not data:
                break  # Client has closed the connection
            print(f"Received from client: {data.decode('utf-8')}")
            # Echo the data back to the client
            conn.sendall(data)
        print("Client disconnected.")
print("Server has shut down.")

To run:

  1. Open a terminal and run the server: python server.py
  2. Open a second terminal and run the client: python client.py

You will see the client connect, send a message, receive an echo, and then close.


Key socket Methods Explained

Method Description
socket.socket(...) Creates a new socket object. You specify the address family (AF_INET for IPv4) and socket type (SOCK_STREAM for TCP).
s.connect((host, port)) Client-side: Initiates a connection to the specified address and port. It will block until the connection is made or fails.
s.bind((host, port)) Server-side: Assigns the socket to a specific network interface and port. This is where the server "lives".
s.listen() Server-side: Enables the server to accept connections. It puts the socket into a "listening" state.
s.accept() Server-side: Blocks and waits for an incoming connection. When a client connects, it returns a new socket object (conn) for that specific connection and the client's address.
s.sendall(data) Sends all the data in the buffer. It continues to send data until all of it has been sent or an error occurs. The data must be bytes.
s.recv(buffer_size) Receives data from the connected socket. It blocks until data is available. The buffer_size is the maximum amount of data to receive at once. Returns bytes.
s.close() Closes the socket. The with statement handles this automatically.

Practical Example: Checking if a Port is Open (Port Scanner)

A very common task is to check if a specific port on a remote host is open (i.e., a service is listening). This is a simplified version of a port scanner.

Python如何连接指定端口?-图3
(图片来源网络,侵删)
# port_scanner.py
import socket
import sys
def check_port(host, port):
    """
    Checks if a specific port is open on a given host.
    Returns True if the port is open, False otherwise.
    """
    # Create a new socket for each check
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # Set a timeout to prevent the script from hanging indefinitely
        s.settimeout(2.0) 
        try:
            # The connect_ex() method is like connect(), but it returns an
            # error indicator (an integer) instead of raising an exception.
            # A successful connection returns 0.
            result = s.connect_ex((host, port))
            if result == 0:
                print(f"Port {port}: OPEN")
                return True
            else:
                # print(f"Port {port}: CLOSED (Error: {result})") # For debugging
                return False
        except socket.timeout:
            print(f"Port {port}: TIMEOUT")
            return False
        except Exception as e:
            print(f"Port {port}: ERROR - {e}")
            return False
if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python port_scanner.py <host> <port>")
        sys.exit(1)
    target_host = sys.argv[1]
    target_port = int(sys.argv[2])
    print(f"Scanning {target_host} on port {target_port}...")
    is_open = check_port(target_host, target_port)
    if is_open:
        print(f"Success! Port {target_port} on {target_host} is open.")
    else:
        print(f"Port {target_port} on {target_host} is not open or not reachable.")

How to Run: python port_scanner.py google.com 80

This will check if port 80 (HTTP) is open on Google's servers. You'll likely see "Port 80: OPEN".


High-Level Libraries: requests for HTTP/HTTPS

For web-related port connections (HTTP/HTTPS on ports 80/443), you should almost always use a high-level library like requests. It abstracts away the raw socket complexity and handles headers, session management, and more.

First, install it: pip install requests

# requests_example.py
import requests
try:
    # The 'http://' or 'https://' prefix tells requests which port to use.
    # http:// -> port 80
    # https:// -> port 443
    url = "https://www.google.com"
    print(f"Connecting to {url}...")
    # The `get()` method handles the entire connection process
    response = requests.get(url, timeout=5)
    # If the request was successful (status code 200-299)
    response.raise_for_status() 
    print(f"Successfully connected! Status Code: {response.status_code}")
    print(f"Response headers: {response.headers['Content-Type']}")
except requests.exceptions.RequestException as e:
    print(f"Failed to connect: {e}")

Summary: Which Method to Use?

Method Best For Pros Cons
socket Low-level networking, custom protocols, learning networking fundamentals. Full control, lightweight, no external dependencies. Complex, you must handle everything (headers, connections, errors).
requests HTTP/HTTPS (web scraping, APIs, REST services). Very easy to use, handles sessions, cookies, redirects, JSON parsing. Only for web protocols, adds external dependency.
ssh, paramiko SSH connections (remote server management, file transfer). Secure, standard for remote command execution. Requires separate libraries (paramiko is popular).
分享:
扫描分享到社交APP
上一篇
下一篇