STM32F103 STM32 4G 模块 A7670 使用Http 协议通讯(二) 搭建本地服务器之html

一: 前言

前文说到通过4G模块上网,但访问的都是已经搭建好的云服务器,那如果云服务器还没搭建好的情况下如何调试呢? 可以通过搭建本地服务器调试。

二: 步骤

  1. 前提: 安装好Python
  2. 运行以下代码文件 http_html.py:
import http.server
import socketserver
import json
import os

# Define the paths for the HTML and POST JSON files
html_file_path = 'index.html'
post_json_file_path = 'post_data.json'

# Ensure the JSON file exists
if not os.path.exists(post_json_file_path):
    with open(post_json_file_path, 'w') as file:
        json.dump({}, file)

class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    protocol_version = 'HTTP/1.1'  # Set the HTTP protocol version to 1.1

    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Date', self.date_time_string())
        self.send_header('Server', 'Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.24')
        self.send_header('X-Powered-By', 'PHP/5.6.24')
        self.send_header('X-UA-Compatible', 'IE=edge,chrome=1')
        self.send_header('Set-Cookie', 'PbootSystem=d7lcbf5eo9fqhnrep7cuv4flf3; path=/; HttpOnly')
        self.send_header('Expires', 'Thu, 19 Nov 1981 08:52:00 GMT')
        self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
        self.send_header('Pragma', 'no-cache')
        self.send_header('Content-Type', 'text/html; charset=utf-8')

        # Calculate content length
        if os.path.exists(html_file_path):
            with open(html_file_path, 'r') as file:
                html_content = file.read()
            content_length = len(html_content.encode('utf-8'))
        else:
            html_content = "<html><body><h1>Hi DongJayYet</h1></body></html>"
            content_length = len(html_content.encode('utf-8'))

        # Send the Content-Length header
        self.send_header('Content-Length', content_length)
        self.end_headers()

    def do_GET(self):
        self.do_HEAD()  # Send headers
        # Write the body
        if os.path.exists(html_file_path):
            with open(html_file_path, 'r') as file:
                html_content = file.read()
            self.wfile.write(html_content.encode('utf-8'))
        else:
            self.wfile.write(b"<html><body><h1>Hi DongJayYet</h1></body></html>")

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        post_data = json.loads(post_data)
        
        # Read the existing data from the POST JSON file
        with open(post_json_file_path, 'r') as file:
            data = json.load(file)
        
        # Update the data with the received POST data
        data.update(post_data)
        
        # Write the updated data back to the POST JSON file
        with open(post_json_file_path, 'w') as file:
            json.dump(data, file, indent=4)
        
        response = {'message': 'Data received and stored', 'received_data': post_data}
        response_data = json.dumps(response).encode('utf-8')
        response_length = len(response_data)

        self.send_response(200)
        self.send_header('Date', self.date_time_string())
        self.send_header('Server', 'Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.24')
        self.send_header('Content-Type', 'application/json; charset=utf-8')
        self.send_header('Content-Length', response_length)
        self.end_headers()

        self.wfile.write(response_data)

PORT = 8000

Handler = MyHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Serving at port", PORT)
    httpd.serve_forever()

运行如下:

server_local_exp>python http_html.py Serving at port 8000 127.0.0.1 - - [03/Jun/2024 09:10:03] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [03/Jun/2024 09:10:03] "GET /favicon.ico HTTP/1.1" 200 -

这里搭建服务器,当有GET访问时,发送特定内容, 并接受Json格式的数据上传。

先在浏览器测试,输入http://localhost:8000/

  1. 在本地访问没有问题,但如果电脑是处于内网,还需要将外网地址映射到本地电脑和端口,一般请网管IT设置一下路由器就可以。
  2. 在stm32端,修改提供的例程:

A. 将例程中URL改为网管给的地址端口: "AT+HTTPPARA=\"URL\",\"http://61.145.188.88:8088/\\\\"\\\\r\\\\n", 1000))

B. POST 命令增加内容为JSON部分,并设置大小为17, 然后是json格式数据:

//设置 HTTP POST 数据
case 0x03:
if (fat_send_wait_cmdres_blocking("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n", 1000))

case 0x04:
if (fat_send_wait_cmdres_blocking("AT+HTTPDATA=17,1000\r\n", 1000))

case 0x05:
if (fat_send_wait_cmdres_blocking("{\"key8\":\"value8\"}", 1000))

三: 测试结果

检测post_data.json文件,可以看到key8 value8 被写入:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值