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

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

Watrt6年前 (2019-12-31)Python21010

响应(服务端)

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

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

相关文章

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

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

pyqt5 开发入门

pyqt5 开发入门

使用:pyqt5-tools designer 进行窗口设计使用命令:pyuic5 -o textedit.py textedit.ui  把设计的UI文件转换成py文件注意:使用对话框的话要继承于class Ui_MainWindow(QtWidgets.QMainWindow):使用信号的方法:self.actionnew.triggered.connect(self.new_btn)    self.new_btn 为当前类下的方法下面示例:#&nb...

word转换服务python版本

word转换服务python版本

网页无法打开word文档。但是可以转换为pdf来在网页上找开。用python实现在线转换服务。from flask import Flask, request, jsonify,redirect,send_from_directory from win32com.client import constants,gencache import requests import os impor...

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("地址不正确...

使用 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全部使用了默认参数,默认进度条样式就是如上所示;通常默认进度条所输出...

发表评论

访客

看不清,换一张

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