<jupyter_start><jupyter_text>osi 五层网络模型（全七层）- 应用层 ftp, http, smtp, pop3, DNS, DHCP...- （表示层 jpeg, ascii, 加密, ...）- （会话层）- 传输层 tcp, udp.- 网络层 IP(v4, v6), icmp,...- 数据链路层- 物理层 socket由系统提供的统一的传输层往下的API<jupyter_code>import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 8000))
server.listen()

while True:
    sock, addr = server.accept()
    
    while True:
        data = sock.recev(1024) 
        print(data.decode('utf-8'))
        in_data = input()
        sock.send(in_data.encode('utf-8'))
        
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8000))
while True:
    in_data = input()
    client.send(in_data.encode('utf-8'))
    data = client.recev(1024)
    print(data.decode('utf-8'))<jupyter_output><empty_output><jupyter_text>socket 模拟 http 请求<jupyter_code>from urllib.parse import urlparse<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16466739367502725, 'fraction_numerical': 0.03816793893129771, 'mean_word_length': 3.857142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '638178', 'license': 'MIT', 'n_tokens_mistral': 420, 'n_tokens_neox': 422, 'n_words': 63, 'path': 'sec8_socket.ipynb', 'repo_name': 'yyearth/adv_py', 'size': '917'}