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

asyncio --- 异步 I/O http服务代码

Watrt5年前 (2021-10-11)Python27450
import asyncio
async def service(reader,writer):

   data = await reader.read(1024) #同步读取数据
   data =data.decode().replace('\r','<br/>')

   print(writer.get_extra_info('peername'))    #获取连接信息
   writer.write(b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n')  #返回HTTP响应头
   #writer.write(b'hi fukun\r\n')
   writer.write(data.encode())     #直接返回响应信息
   writer.close()
   #print(data)


async def server_run():
   server = await asyncio.start_server(service,'127.0.0.1',8888)
   addr = server.sockets[0].getsockname()
   print(f'Serving on {addr}')

   async with server:
       await server.serve_forever()    #开始连接

asyncio.run(server_run())

在linux python3.8测试通过。

import uasyncio
import gc

async def service(reader,writer):
  try:
    data = await reader.read(1024)
    print(data)
    await writer.awrite('HTTP/1.1 200 ok\r\n\r\n hi fukun'.encode())
    await writer.awrite(data)
  except:
    print("传输错误!")
  finally:
    await writer.aclose()
  gc.collect()
def start():
  
  loop = uasyncio.get_event_loop()        //获取协议
  #loop.call_soon(uasyncio.start_server(service, "0.0.0.0", 80))
  loop.create_task(uasyncio.start_server(service, "0.0.0.0", 80))
  loop.run_forever()            
  loop.close()

uasyncio.run(start())

通过micropython 实现

打赏 支付宝打赏 微信打赏

相关文章

esp32外部中断学习笔记

esp32外部中断学习笔记

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

micropython中使用rtc设置时间

micropython中使用rtc设置时间

micropython中使用rtc设置时间from machine import RTC rtc = RTC() rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time print(rtc.datetime(...

micropython 使用ili9163显示IP地址

micropython 使用ili9163显示IP地址

mian.pyexec(open('./wifi.py').read(),globals()) exec(open('./tft.py').read(),globals())wifi.pyimport network import machine import ntptime import time nic = network.WLAN(network.STA_IF) # c...

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