当前位置:首页 > 技术 > Python > 正文内容

python编写的简单http请求和应答

Watrt5年前 (2019-12-31)Python15770

响应(服务端)

import machine
import socket
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]
adc = machine.ADC(0)
html = """<!DOCTYPE html>
<html>
    <head> <title>ESP8266 Pins</title> </head>
    <body> <h1>ESP8266 Pins</h1>
        <table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s 
        <tr><td>ad0</td><td>%d</td></tr>
        </table>
    </body>
<script type="text/javascript">
setTimeout(function(){  //使用  setTimeout()方法设定定时2000毫秒
window.location.reload();//页面刷新
},1000);
</script>
</html>
"""
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(3)
print('listening on', addr)
while True:
    cl, addr = s.accept()
    print('client connected from', addr)
    cl_file = cl.makefile('rwb', 0)
    while True:
        line = cl_file.readline()
        if not line or line == b'\r\n':
            break
    rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
    response = html % ('\n'.join(rows),adc.read())
    cl.send(response)
    cl.close()

请求(客户端)

try:
    import usocket as socket
except:
    import socket
def main(use_stream=False):
    s = socket.socket()
    ai = socket.getaddrinfo("028sd.net", 80)
    print("Address infos:", ai)
    addr = ai[0][-1]
    print("Connect address:", addr)
    request_url = 'GET / HTTP/1.1\r\nHost: 028sd.net\r\nConnection: close\r\n\r\n'
    s.connect(addr)
    if use_stream:
        s = s.makefile("rwb", 0)
        s.write(request_url.encode())
        print(s.read(2048))
    else:
        s.send(request_url.encode())
        print(s.recv(2048).decode('gbk'))
    s.close()
main()

代码是用于micropython上面使用的。

分享给朋友:

相关文章

python利用pyinstaller打包简明教程

python利用pyinstaller打包简明教程

在创建了独立应用(自包含该应用的依赖包)之后,还可以使用 PyInstaller 将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的 Windows 或 Mac OS X 平台上运行。安装 PyInstallePython 默认并不包含 PyInstaller 模块,因此需要自行安装 PyInstaller 模块。安装 PyInstaller 模块与安装其他 Python 模块一样,使用 pip 命令安装即可。在命令行输入如下命令:pip install ...

Python使用struct处理二进制(pack和unpack用法)

Python使用struct处理二进制(pack和unpack用法)

有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重要的三个函数是pack(), unpack(), calcsize()pack(fmt, v1, v2, ...)     按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流) un...

micropython解码bmp

micropython解码bmp

from ST7735 import TFT,TFTColor from machine import SPI,Pin spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12)) tft=TFT(spi,16,17,18)...

micropython 清除flash磁盘内容

micropython 清除flash磁盘内容

对于ESP32,一般是在命令行中使用esptool.py清除flash,如果是在程序中想清除flash内容,或者更换文件系统格式,可以用下面方法:import uos uos.VfsFat.mkfs(bdev)如果使用 LFS2 文件系统,命令是import uos uos.VfsLfs2.mkfs(bdev)对于STM32,默认没有bdev设备,需要使用下面方法:import uos flash = pyb.Flash(start=0)u...

python使用requests库下载文件

python使用requests库下载文件

import os import requests def downfile(url): try: url=url.split("@")[0].split("?")[0] urlfile=url.split("/")[-1] print(urlfile) except Exception as e: print("地址不正确...

Python内置函数

Python内置函数

以下是 Python 中的全部内置函数,并列出了它们的作用和参数说明。请注意,以下列出的内置函数是基于 Python 3.9 版本。abs(): 返回一个数的绝对值。参数:abs(x)all(): 如果可迭代对象中的所有元素都为 True,则返回 True。参数:all(iterable)any(): 如果可迭代对象中的任何元素为 True,则返回 True。参数:any(iterable)ascii(): 返回一个包含 ASCII 转义字符的字符串表示。参数:ascii(object)bin(...

使用 tqdm 库在控制台中实现进度条

使用 tqdm 库在控制台中实现进度条

简单用法:from tqdm import tqdm  for i in tqdm(range(2)):   pass100%|███████████████████| 2/2 [00:00<00:00, 1998.72it/s]  从上面可以看到生成一个长度为2的列表传入tqdm中,在for中迭代,此时输出了进度条,这里tqdm全部使用了默认参数,默认进度条样式就是如上所示;通常默认进度条所输出...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。