qile1
2017-08-16 01:53:39 +08:00
我简单写了一个测试程序,直接控制本地 ecg 程序打开一个文件后检测关闭状态进行后期处理
’‘’
# -*- coding: utf-8 -*-
from http.server import HTTPServer, BaseHTTPRequestHandler
import io, shutil
import re # 引入正则表达式对象
import urllib # 用于对 URL 进行编解码
import os, os.path, sys
import win32process, win32event
mainfile = os.getcwd()
class TestHTTPHandler(BaseHTTPRequestHandler):
# 处理 GET 请求
def do_GET(self):
# 页面输出模板字符串
templateStr ='''
<html><head><title>QR Link Generator</title></head><body>%s<br><br><form action="/qr" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr></form><a href='/runtest?DataRecive20160706185117'>http://127.0.0.1:8080/runtest?DataRecive20160706185117</a></body></html> '''
# 将正则表达式编译成 Pattern 对象
pattern = re.compile(r'/qr\?s=([^\&]+)\&qr=Show\+QR')
# 使用 Pattern 匹配文本,获得匹配结果,无法匹配时将返回 None
match = pattern.match(self.path)
qrImg = ''
teststr=match
teststr2=(self.path)
teststr3=teststr2.split('\\')[0]
runname=teststr2[1:teststr2.find('?')]
xmlfilename=teststr2[teststr2.find('?')+1:]
if (runname=='runtest'):
exe_path = 'D:\pythontest\ecgfiles' # sys.argv[1]
exe_file = 'ECGView.exe' # '.argv[2]
ECGPath=os.path.join(mainfile, "ecgfiles", "ECGPath.dir")
f = open(ECGPath, "w") # 覆写模式
f.write("D:\\pythontest\\ecgfiles\\ecgdata\\"+xmlfilename+".xml")
f.close()
try:
handle = win32process.CreateProcess(os.path.join(exe_path, exe_file),
'', None, None, 0,
win32process.CREATE_NO_WINDOW,
None,
exe_path,
win32process.STARTUPINFO())
running = True
except Exception as e:
print("Create Error!")
handle = None
running = False
while running:
rc = win32event.WaitForSingleObject(handle[0], 1000)
if rc == win32event.WAIT_OBJECT_0:
running = False
boday1=(templateStr % qrImg)
enc = "UTF-8"
encoded = ''.join(boday1).encode(enc)
f = io.BytesIO()
f.write(encoded)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
httpd = HTTPServer(('', 8080), TestHTTPHandler)
print("Server started on 127.0.0.1,port 8080.....")
httpd.serve_forever()
‘’‘