@app.route('/restruct', methods=['POST']) def restruct(): dch = request.form['dch'] dch_folder = "restr" + dch count = 1 while os.path.exists(os.path.join('D:\cj', dch_folder)): dch_folder = "restr" + dch + "_" + str(count) count += 1 os.makedirs(os.path.join('D:\cj', dch_folder, 'photos')) os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls')) os.makedirs(os.path.join('D:\cj', dch_folder, 'project')) photos_folder = os.path.join('D:\cj', dch_folder, 'photos') kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls') project_folder = os.path.join('D:\cj', dch_folder, 'project') for file in request.files.getlist('files'): filename = file.filename file.save(os.path.join(photos_folder, filename)) dhs = request.form.getlist('dh') for dh in dhs: src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml') dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml') shutil.copy(src_path, dst_path) subprocess.Popen(["python", "restruct.py", dch, photos_folder, project_folder, kmls_folder])使用python3.6版本的websocket,将这段代码运行中的输出信息,实时发送给前端,给出server.py和client.py
时间: 2023-06-24 22:09:01 浏览: 125
Fingerprint_WBF_USB_DCH_ELAN_Z_V3.5.12201.11001_24387.exe
server.py:
```python
import asyncio
import websockets
import subprocess
async def echo(websocket, path):
dch = await websocket.recv()
dch_folder = "restr" + dch
count = 1
while os.path.exists(os.path.join('D:\cj', dch_folder)):
dch_folder = "restr" + dch + "_" + str(count)
count += 1
os.makedirs(os.path.join('D:\cj', dch_folder, 'photos'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'kmls'))
os.makedirs(os.path.join('D:\cj', dch_folder, 'project'))
photos_folder = os.path.join('D:\cj', dch_folder, 'photos')
kmls_folder = os.path.join('D:\cj', dch_folder, 'kmls')
project_folder = os.path.join('D:\cj', dch_folder, 'project')
async for file in websocket:
filename = file.filename
file.save(os.path.join(photos_folder, filename))
dh = await websocket.recv()
src_path = os.path.join(r'D:/911kml/', dh + '.xlsx.kml')
dst_path = os.path.join(r'D:/cj', dch_folder, 'kmls/', dh + '.xlsx.kml')
shutil.copy(src_path, dst_path)
process = await asyncio.create_subprocess_exec(
"python", "restruct.py", dch, photos_folder, project_folder, kmls_folder,
stdout=asyncio.subprocess.PIPE
)
while True:
output = await process.stdout.readline()
if output == b'':
break
await websocket.send(output.decode())
async def main():
async with websockets.serve(echo, "localhost", 8765):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
```
client.py:
```python
import asyncio
import websockets
import aiohttp
async def upload_files(dch, files, dh):
async with aiohttp.ClientSession() as session:
async with session.ws_connect('ws://localhost:8765') as ws:
await ws.send(dch)
for file in files:
await ws.send(file)
await ws.send(dh)
async for message in ws:
print(message)
async def main():
dch = 'example'
files = ['example1.jpg', 'example2.jpg']
dh = 'example'
await upload_files(dch, files, dh)
if __name__ == "__main__":
asyncio.run(main())
```
阅读全文