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

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

Watrt6年前 (2019-12-31)Python19300

响应(服务端)

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上面使用的。

打赏 支付宝打赏 微信打赏
分享给朋友:

相关文章

esp32外部中断学习笔记

esp32外部中断学习笔记

思路:我使用的esp32开发板为简易的开发板,因为板上没有用户自己定义的键盘所以在使用中想要输入或者控制无法完成。研究发现在开发板上有的下载按钮是不是可以在进入系统后复用为一个按键呢。实现:参考官方的例程对代码如下首先宏定义参数:#define KYE_0   0    //GPIO0检测 #define KEYS_SET   (1ULL<<0) &nb...

micropython 使用oled显示前面设置的时间。

micropython 使用oled显示前面设置的时间。

from machine import I2C, Pin import ssd1306,time    #这里引用的是官方的ssd1306库。可以在github上下载下来放入库中。 i2c = I2C(-1, Pin(14), Pin(2)) display = ssd1306.SSD1306_I2C(128, 64, i...

micropython连接到wifi上面

micropython连接到wifi上面

import network,time,ujson,urequests,ntptime from machine import RTC nic=network.WLAN(network.STA_IF) nic.active(True) #nic.connect('ZCWH','00004157') nic.connect('028sd.com','88884444') nic.co...

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 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle

micropython 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle

micropython 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle 错误解决方法。出现错误的原因是ssl中的证书过期了。解决办法:1:(Top) > Component config > mbedTLS > Certificate Bundle->Enable trusted root certificate bundle   禁用2:修改配置文件 :sdkconfig.base  中添加入...

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(...

发表评论

访客

看不清,换一张

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