R1:T | t.CPtr -> T*(40 文件 / 1121 处;含多级 **、强转位 (T*)(x)、下标内 list[AST*]);引号前向引用 'Rect' | t.CPtr 26 处按例外保留,T | t.CPtr | t.State 25 处因 T* 后不能接 | 保持原联合写法。 R12:i = 0 + while i < N: + 尾 i += 1 -> or i in range(N):(10 文件 / 42 处;含 <= -> range(EXPR + 1)、带注解初始化 i: int = 0、尾 i = i + 1)。余 20 处经守卫判定不可安全改写(复合条件 1、尾部非自增 8、体内重绑定/continue 5、while-else 2、循环后仍被读取 2、找不到初始化 2)。
81 lines
2.6 KiB
Plaintext
81 lines
2.6 KiB
Plaintext
import t, c
|
||
from stdint import *
|
||
import stdio
|
||
import stdlib
|
||
import socket
|
||
import memhub
|
||
import requests
|
||
|
||
|
||
# 全局 MBuddy 指针
|
||
_mbuddy: memhub.MemBuddy*
|
||
|
||
|
||
@t.CExport
|
||
def main() -> int:
|
||
global _mbuddy
|
||
stdio.printf("WebTest: HTTP client library test\n")
|
||
|
||
# 初始化内存管理器(16MB arena)
|
||
arena: bytes = stdlib.malloc(16 * 1024 * 1024)
|
||
if arena is None:
|
||
stdio.printf("FATAL: malloc arena failed\n")
|
||
return 1
|
||
|
||
_mbuddy = memhub.MemBuddy(arena, 16 * 1024 * 1024)
|
||
requests._mbuddy = _mbuddy
|
||
|
||
# 初始化 Socket 库
|
||
result: INT = socket.SocketInit()
|
||
stdio.printf("SocketInit: %d\n", result)
|
||
|
||
# 测试 Socket OOP API
|
||
sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
if sock.fd != socket.INVALID_SOCKET:
|
||
stdio.printf("Socket: OK (fd=%d)\n", sock.fd)
|
||
sock.close()
|
||
stdio.printf("Socket.close: OK\n")
|
||
else:
|
||
stdio.printf("Socket: FAIL\n")
|
||
|
||
# 测试 DNS 解析 — 通过 Socket.connect 间接测试(内部调用 _MakeAddr)
|
||
stdio.printf("\n--- DNS test ---\n")
|
||
dns_sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
if dns_sock.fd != socket.INVALID_SOCKET:
|
||
dns_sock.settimeout(5000)
|
||
result: INT = dns_sock.connect("cn.bing.com", 80)
|
||
if result == 0:
|
||
stdio.printf("DNS: cn.bing.com resolved OK\n")
|
||
else:
|
||
stdio.printf("DNS: cn.bing.com resolved but connect failed (err in _MakeAddr debug above)\n")
|
||
dns_sock.close()
|
||
|
||
# 测试 HTTP GET — 使用 DNS 解析 cn.bing.com(example.com 在部分网络下不可达)
|
||
stdio.printf("\n--- HTTP GET test (cn.bing.com) ---\n")
|
||
resp = requests.get("http://cn.bing.com/")
|
||
if resp is not None:
|
||
stdio.printf("requests.get: OK\n")
|
||
code: INT = resp.status_code
|
||
length: INT = resp.body_len
|
||
stdio.printf(" status_code: %d\n", code)
|
||
stdio.printf(" body_len: %d\n", length)
|
||
if resp.text is not None and length > 0:
|
||
print_len: INT = 300
|
||
if length < print_len:
|
||
print_len = length
|
||
stdio.printf(" text (first %d chars):\n", print_len)
|
||
txt: str = resp.text
|
||
i: INT = 0
|
||
while i < print_len and txt[i] != 0:
|
||
stdio.printf("%c", txt[i])
|
||
i += 1
|
||
stdio.printf("\n")
|
||
requests.release(resp)
|
||
else:
|
||
stdio.printf("requests.get: FAIL (network unavailable)\n")
|
||
|
||
# 清理
|
||
socket.SocketCleanup()
|
||
stdio.printf("\nSocketCleanup: OK\n")
|
||
stdio.printf("WebTest: ALL PASSED\n")
|
||
return 0 |