HTTP 是一种用于分布式、协作式和超媒体信息系统的应用层协议。它是万维网数据通信的基础。
GET
:请求资源。POST
:提交数据以处理请求。PUT
:上传文件。DELETE
:删除资源。HEAD
:获取头部信息。OPTIONS
:询问服务器支持的 HTTP 方法。PATCH
:部分更新资源。以下是一个简单的 Python 代码示例,演示 HTTP 服务器和客户端的通信:
from http.server import BaseHTTPRequestHandler, HTTPServer
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Hello from server')
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
server_address = ('', 8080)
httpd = server_class(server_address, handler_class)
print('Server running on port 8080')
httpd.serve_forever()
if __name__ == '__main__':
run()
import requests
def http_client():
response = requests.get('http://127.0.0.1:8080')
print(f'Status code: {response.status_code}')
print(f'Response body: {response.text}')
if __name__ == '__main__':
http_client()
TCP 是一种面向连接的、可靠的、基于字节流的传输层通信协议。
以下是一个简单的 Python 代码示例,演示 TCP 客户端和服务器的通信:
import socket
def tcp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 8080))
server_socket.listen(5)
print('Server listening on port 8080')
while True:
client_socket, addr = server_socket.accept()
print(f'Connection from {addr}')
data = client_socket.recv(1024)
print(f'Received data: {data.decode()}')
client_socket.sendall(b'Hello from server')
client_socket.close()
if __name__ == '__main__':
tcp_server()
import socket
def tcp_client():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 8080))
client_socket.sendall(b'Hello from client')
data = client_socket.recv(1024)
print(f'Received data: {data.decode()}')
client_socket.close()
if __name__ == '__main__':
tcp_client()
UDP 是一种无连接的、不可靠的传输层协议。
以下是一个简单的 Python 代码示例,演示 UDP 客户端和服务器的通信:
import socket
def udp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', 8080))
print('Server listening on port 8080')
while True:
data, addr = server_socket.recvfrom(1024)
print(f'Received data: {data.decode()} from {addr}')
server_socket.sendto(b'Hello from server', addr)
if __name__ == '__main__':
udp_server()
import socket
def udp_client():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.sendto(b'Hello from client', ('127.0.0.1', 8080))
data, addr = client_socket.recvfrom(1024)
print(f'Received data: {data.decode()}')
client_socket.close()
if __name__ == '__main__':
udp_client()
IP 是一种负责在源地址和目的地址之间传输数据包的网络层协议。
以下是一个简单的 Python 代码示例,用于解析 IP 数据包:
import socket
import struct
def parse_ip_packet(packet):
ip_header = packet[:20]
iph = struct.unpack('!BBHHHBBH4s4s', ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
ttl = iph[5]
protocol = iph[6]
src_address = socket.inet_ntoa(iph[8])
dest_address = socket.inet_ntoa(iph[9])
print(f'Version: {version}, Header Length: {ihl*4} bytes, TTL: {ttl}')
print(f'Protocol: {protocol}, Source Address: {src_address}, Destination Address: {dest_address}')
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind(("0.0.0.0", 0))
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
packet, addr = s.recvfrom(65565)
parse_ip_packet(packet)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。