winXP下构建python开发环境

近期车间有个动平衡检测仪数采的需求,工控机是xp系统,原理也很简单,监控文件变化,发现有新的检测数据就调用远程接口传输到服务器上去。
通常python监控文件变化会用watchdog这个库, 可是xp太老了,测试了一下午没找到合适的watchdog版本,在win11下倒是测通了。 于是换了另外一个思路。 通过轮询监控文件的大小,发现大小变化了就读取最新的内容,通过requests库调用远程接口。代码如下:

import logging
import os
import time,datetime
import json
import requests



logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s")
log = logging.getLogger('logger')


# 配置文件路径,假设配置文件是JSON格式
CONFIG_FILE_PATH = 'config.json'


# 读取配置文件中的监测文件路径和服务器接口

with open(CONFIG_FILE_PATH, 'r',encoding="utf-8") as f:
    config = json.load(f)
ok_file_path = config['ok_file_path']
ng_file_path = config['ng_file_path']
server_api_url = config['server_api_url']
device_id = config['device_id']
interval = int(config['interval'])

# 读取文件末尾的位置
f_ok = open(ok_file_path, 'r')
f_ok.seek(0, os.SEEK_END)  # 将流位置改为末尾
ok_file_size = os.path.getsize(ok_file_path)# 获取文件当前的大小

f_ng = open(ng_file_path, 'r')
f_ng.seek(0, os.SEEK_END)  # 将流位置改为末尾
ng_file_size = os.path.getsize(ng_file_path)# 获取文件当前的大小

def setlog():
    logger = logging.getLogger('logger')


    rf_handler = logging.FileHandler('log/upload.log')
    rf_handler.setLevel(logging.DEBUG)
    rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))

    #debug_handler = logging.handlers.
    f_handler = logging.FileHandler('log/upload_error.log')
    f_handler.setLevel(logging.ERROR)
    f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))

    logger.addHandler(rf_handler)
    logger.addHandler(f_handler)

# 发送文件内容到服务器
def send_to_server(data,result):
    global device_id
    global server_api_url
    params = {'device_id': device_id, 'result': result, 'data': data}
    response = requests.get(server_api_url, params=params)
    if response.status_code == 200:
        log.info("文件内容已发送到服务器data=" + str(params))
    else:
        log.error("发送失败,状态码:", response.status_code)


# 监控文件变化
def monitor_log_file():
    global ok_file_size
    global ng_file_size
    while True:
        # 检查文件大小是否有变化
        current_size = os.path.getsize(ok_file_path)
        if current_size > ok_file_size:
            # 读取新增的内容
            ok_file_size = current_size
            new_content = f_ok.read().strip()
            log.info(new_content)
            send_to_server(new_content,'OK')

        current_size = os.path.getsize(ng_file_path)
        if current_size > ng_file_size:
            # 读取新增的内容
            ng_file_size = current_size
            new_content = f_ng.read().strip()
            log.info(new_content)
            send_to_server(new_content,'NG')

        time.sleep(interval)  # 适当延时,避免过度占用资源


if __name__ == "__main__":
    log.info("Monitoring log file changes in:" + ok_file_path + ' ' + ng_file_path)
    monitor_log_file()

config.json 内容如下:

{
    "ng_file_path": "NGData01.tj",
    "ok_file_path": "FjokData01.tj",
    "server_api_url": "http://192.168.1.215/hxdcs_balance/data",
    "device_id": "1",
    "interval": "2"
}

代码其实不复杂,构建xp的开发环境比较烦。因为xp是32位的,现在大部分机器都是64位的,所以不能用64位的系统打包程序。

1、安装winxp虚拟机

下载可以光盘引导的winxp安装盘
https://msdn.itellyou.cn
序列号:MRX3F-47B9T-2487J-KWKMF-RPWBY
下载标出来的两个光盘。
在这里插入图片描述

2、安装python

winxp支持的最高版本的python是3.4.4,所以只能下载3.4版本的python。 因为这个限制,后面很多包都收到python版本的限制。
https://www.python.org/downloads/windows/
找到3.4.4 下载
安装完成后将C:\Python34\Scripts 和C:\Python34 加入path环境变量。

3、安装pycharm

其实pycharm不好太好的选择,太占资源了。 只能下载下面这个版本的。
PyCharm 2016.1.5社区版下载链接:https://www.jetbrains.com/pycharm/download/other.html

4、安装python包

代码只用到requests 和pyinstaller这两个库,可是pip用不了,所以依赖问题要自己解决。
到pypi的网站上手工下载老版本。其中

requests-2.5.0-py2.py3-none-any.whl

pyinstaller依赖比较多

altgraph 0.17
future 0.18.2
macholib 1.14
pefile 2019.4.18
pywin32-ctypes 0.2.0
wheel 0.33.6
PyInstaller-3.4.tar

手工通过python -m pip install package_name 一个一个安装
最后通过pyinstaller -F send_data.py 进行单文件打包。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值