cs-networks
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecs-networks
cs-networks
Purpose
用途
This skill equips the AI to handle computer network protocols and programming, focusing on TCP/IP, HTTP/2/3, TLS 1.3, DNS, BGP, WebSockets, QUIC, and socket operations for building, debugging, and securing networked applications.
本技能让AI能够处理计算机网络协议和编程,重点覆盖TCP/IP、HTTP/2/3、TLS 1.3、DNS、BGP、WebSockets、QUIC和socket操作,可用于构建、调试和保障网络应用的安全。
When to Use
适用场景
Use this skill for tasks involving network communication, such as establishing connections in distributed systems, troubleshooting protocol issues (e.g., HTTP timeouts), implementing secure data transfer with TLS, or optimizing performance with QUIC in web apps. Apply it when code requires socket programming or protocol-specific handling, like DNS resolution in IoT devices.
本技能适用于涉及网络通信的任务,比如在分布式系统中建立连接、排查协议问题(例如HTTP超时)、使用TLS实现安全数据传输,或者在Web应用中使用QUIC优化性能。当代码需要进行socket编程或协议特定处理时(比如IoT设备中的DNS解析),也可以使用本技能。
Key Capabilities
核心能力
- Establish and manage TCP/IP connections, including creating sockets and handling data streams.
- Process HTTP/2 and HTTP/3 requests, including multiplexing and header compression.
- Implement TLS 1.3 encryption for secure sockets, verifying certificates and cipher suites.
- Perform DNS lookups and resolve hostnames using standard libraries.
- Handle BGP routing concepts for network topology analysis.
- Manage WebSockets for real-time bidirectional communication.
- Optimize with QUIC for low-latency UDP-based transports.
- Conduct socket programming for custom protocols, including error detection and retries.
- 建立和管理TCP/IP连接,包括创建socket和处理数据流
- 处理HTTP/2和HTTP/3请求,包括多路复用和头部压缩
- 为安全socket实现TLS 1.3加密,验证证书和密码套件
- 使用标准库执行DNS查找和主机名解析
- 掌握BGP路由概念,用于网络拓扑分析
- 管理WebSockets,实现实时双向通信
- 使用QUIC优化低延迟基于UDP的传输
- 为自定义协议进行socket编程,包括错误检测和重试
Usage Patterns
使用模式
To use this skill, integrate it into code by importing relevant libraries (e.g., Python's socket or requests). Start with initializing connections, then handle data exchange in loops, and finally clean up resources. For HTTP operations, use asynchronous patterns to avoid blocking. Always check for authentication requirements early, like setting $API_KEY for external APIs. Pattern: Import module -> Configure options (e.g., timeouts) -> Execute operation -> Handle response or errors.
要使用本技能,可通过导入相关库(比如Python的socket或requests)将其集成到代码中。首先初始化连接,然后循环处理数据交换,最后清理资源。对于HTTP操作,使用异步模式避免阻塞。始终提前检查身份验证要求,比如为外部API设置$API_KEY。模式:导入模块 -> 配置选项(例如超时时间) -> 执行操作 -> 处理响应或错误。
Common Commands/API
常用命令/API
- Socket Programming (Python): Create a TCP socket and connect:
python
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('example.com', 80)) - HTTP Requests (using requests library): Send a GET request with HTTP/2:
python
import requests response = requests.get('https://api.example.com/data', headers={'Authorization': f'Bearer {os.environ.get("API_KEY")}'}) - DNS Lookup (Python): Resolve a hostname:
python
import socket address = socket.gethostbyname('example.com') print(address) # e.g., '93.184.216.34' - TLS Verification: Use OpenSSL CLI for testing: to verify TLS 1.3 handshake.
openssl s_client -connect example.com:443 -tls1_3 - WebSockets: Establish a connection with websockets library:
python
import websockets async with websockets.connect('ws://example.com/ws') as websocket: await websocket.send('Hello') - QUIC/HTTP/3: Test with curl: (ensure QUIC support in curl version).
curl --http3 https://example.com -H 'Authorization: Bearer $QUIC_API_KEY' - BGP Query: Use bgpq3 tool: to fetch prefixes for an AS number.
bgpq3 -A AS1234 -l myprefix
- Socket编程(Python):创建TCP套接字并建立连接:
python
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('example.com', 80)) - HTTP请求(使用requests库):发送HTTP/2 GET请求:
python
import requests response = requests.get('https://api.example.com/data', headers={'Authorization': f'Bearer {os.environ.get("API_KEY")}'}) - DNS查找(Python):解析主机名:
python
import socket address = socket.gethostbyname('example.com') print(address) # e.g., '93.184.216.34' - TLS验证:使用OpenSSL CLI测试:验证TLS 1.3握手。
openssl s_client -connect example.com:443 -tls1_3 - WebSockets:使用websockets库建立连接:
python
import websockets async with websockets.connect('ws://example.com/ws') as websocket: await websocket.send('Hello') - QUIC/HTTP/3:使用curl测试:(确保curl版本支持QUIC)。
curl --http3 https://example.com -H 'Authorization: Bearer $QUIC_API_KEY' - BGP查询:使用bgpq3工具:获取AS号的前缀列表。
bgpq3 -A AS1234 -l myprefix
Integration Notes
集成注意事项
Integrate this skill by wrapping network operations in functions or classes for modularity. For authenticated APIs, set environment variables like and access via in Python. Handle TLS by specifying CA bundles, e.g., in requests: . For QUIC, ensure the environment supports UDP (e.g., no firewalls blocking port 443). Config formats: Use JSON for API configs, e.g., . Avoid hardcoding keys; use vaults or env vars for security.
export API_KEY=your_secret_keyos.environ.get('API_KEY')requests.get(url, verify='/path/to/ca.crt'){"host": "example.com", "port": 443, "tls": true}将网络操作封装在函数或类中以实现模块化,从而集成本技能。对于需要身份验证的API,设置环境变量如,并在Python中通过访问。通过指定CA bundle处理TLS,比如在requests中:。对于QUIC,确保环境支持UDP(例如没有防火墙阻止443端口)。配置格式:API配置使用JSON,例如。避免硬编码密钥;使用密钥保管库或环境变量保障安全。
export API_KEY=your_secret_keyos.environ.get('API_KEY')requests.get(url, verify='/path/to/ca.crt'){"host": "example.com", "port": 443, "tls": true}Error Handling
错误处理
Always wrap network operations in try-except blocks to catch exceptions. For sockets, check specific errors:
python
import socket
try:
s.connect(('example.com', 80))
except socket.error as e:
if e.errno == socket.errno.ECONNREFUSED:
print("Connection refused; check server status")
elif e.errno == socket.errno.ETIMEDOUT:
print("Timeout; retry with exponential backoff")For HTTP, handle status codes: if response.status_code == 401, prompt for $API_KEY refresh. In QUIC/HTTP/3, watch for h3 errors like stream resets. Implement retries with limits, e.g., using tenacity: . Log errors with details like errno for debugging.
@retry(stop=stop_after_attempt(3), wait=wait_exponential_multiplier(1)) def fetch_data(): ...始终将网络操作包裹在try-except块中捕获异常。对于socket,检查特定错误:
python
import socket
try:
s.connect(('example.com', 80))
except socket.error as e:
if e.errno == socket.errno.ECONNREFUSED:
print("Connection refused; check server status")
elif e.errno == socket.errno.ETIMEDOUT:
print("Timeout; retry with exponential backoff")对于HTTP,处理状态码:如果response.status_code == 401,提示刷新$API_KEY。在QUIC/HTTP/3中,注意流重置等h3错误。实现带次数限制的重试,比如使用tenacity:。记录包含errno等细节的错误以便调试。
@retry(stop=stop_after_attempt(3), wait=wait_exponential_multiplier(1)) def fetch_data(): ...Concrete Usage Examples
具体使用示例
-
Example: Implement a Simple TCP Client
To send data over TCP, use this code:pythonimport socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 8080)) s.sendall(b'Hello, server') response = s.recv(1024) s.close()This connects to a local server, sends a message, and receives a response. Use it for testing network apps; ensure the server is running on port 8080. -
Example: Secure HTTP Request with TLS
To fetch data from a TLS-protected endpoint:pythonimport requests headers = {'Authorization': f'Bearer {os.environ.get("API_KEY")}'} response = requests.get('https://api.example.com/data', headers=headers, verify=True) if response.ok: print(response.json())This retrieves JSON data while enforcing TLS verification. Set $API_KEY in your environment before running, and handle 4xx/5xx errors as shown in Error Handling.
-
示例:实现简单TCP客户端
要通过TCP发送数据,使用以下代码:pythonimport socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 8080)) s.sendall(b'Hello, server') response = s.recv(1024) s.close()这段代码连接到本地服务器,发送消息并接收响应。可用于测试网络应用;确保服务器在8080端口运行。 -
示例:使用TLS的安全HTTP请求
从受TLS保护的端点获取数据:pythonimport requests headers = {'Authorization': f'Bearer {os.environ.get("API_KEY")}'} response = requests.get('https://api.example.com/data', headers=headers, verify=True) if response.ok: print(response.json())这段代码在强制TLS验证的同时获取JSON数据。运行前在环境中设置$API_KEY,并按照错误处理部分的说明处理4xx/5xx错误。
Graph Relationships
关联关系
- Related Clusters: computer-science (parent cluster for this skill).
- Related Skills: cs-algorithms (for network pathfinding), cs-security (for TLS and encryption integration).
- Tags Connections: "networks" links to cs-security and general-computing; "tcp" and "http" connect to web-development skills; "tls" and "dns" relate to cs-infrastructure.
- 关联集群:computer-science(本技能的父集群)
- 关联技能:cs-algorithms(用于网络路径查找)、cs-security(用于TLS和加密集成)
- 标签关联:"networks"关联到cs-security和通用计算;"tcp"和"http"关联到Web开发技能;"tls"和"dns"关联到cs-infrastructure。