C# 启动 a Python Web Server with Flask
概览
最近有个需求是通过c#代码来启动 python 脚本。嘿~嘿!!!
突发奇想~~既然可以启动 python 脚本,那也能启动 flask,于是开始着手操作。
先看gif图

准备
因为使用的是.NET Core 3.1来创建的控制台程序,启动flask web程序,所以需要下载dotnet-sdk-3.1, 如果使用的是.Net Framework直接运行即可,当前是生产环境下的启动,如果是开发机,直接使用虚拟环境即可无需配置。
大部分客户机系统安装的都是纯净版本,往往缺少一部分组件,所以我们在纯净的环境下需要准备必备组件:
dotnet-sdk-3.1.100-win-x64.exe
实现
通过Process来启动flask的server.py
cmd
cd Desktop
mkdir test
cd test
code .
ctrl + j
dotnet new console
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace tes
{
class Program
{
private static string _basePath => AppDomain.CurrentDomain.BaseDirectory;
private static string _srcPath => Path.Combine(_basePath, @"AnWorker\src");
private static string _venvPath => Path.Combine(_basePath, @"AnWorker\venv");
static void Main(string[] args)
{
// string fileName = Path.Combine(_venvPath, "Scripts", "python.exe");
// string arguements = Path.Combine(_srcPath, "server.py");
var workPath = Path.Combine(_venvPath, "Scripts");
var fileName = Path.Combine(workPath, "python.exe");
var arguements = Path.Combine(_srcPath, "server.py");
Console.WriteLine(fileName);
Console.WriteLine(arguements);
var psi = new ProcessStartInfo(fileName)
{
Arguments = arguements,
WorkingDirectory = _venvPath,
ErrorDialog = false,
UseShellExecute = false
};
var path = psi.EnvironmentVariables["PATH"];
if (path != null)
{
var arrayt = path.Split(new[] { ';' }).Where(p => !p.ToLower().Contains("python")).ToList();
arrayt.AddRange(new[] { _venvPath, Path.Combine(_venvPath, "Lib") });
psi.EnvironmentVariables["PATH"] = string.Join(";", arrayt);
}
Process.Start(psi);
Console.ReadLine();
}
}
}
图1
图2
结构与配置
AnWorker 是脚本网站的代码,结构如下(看图1图2)
- venv 是虚拟环境,学过Python的都明白
- /venv/Lib 把Python 3的系统依赖库(Python\Python37\Lib 此文件夹下 除 site-packages 文件夹)存放在里面,不然启动不成功

- /venv/DLLs (不然则会缺少一大堆的模块在路上)

- /venv/Scripts 替换掉原有的虚拟环境

- /venv/Lib 把Python 3的系统依赖库(Python\Python37\Lib 此文件夹下 除 site-packages 文件夹)存放在里面,不然启动不成功
- doc 是存放文档
- src 是源码
结束
阐述下自己的观点,看官可能会问到干嘛不直接F5,其不是更好,我想说:存在即合理
现在Python应用很广,任何语言都有可能会使用到脚本开发。
这个时候,C# 调用Python 应运而生!
安排~
C# 启动 a Python Web Server with Flask的更多相关文章
- 小测几种python web server的性能
http://blog.csdn.net/raptor/article/details/8038476 因为换了nginx就不再使用mod_wsgi来跑web.py应用了,现在用的是gevent-ws ...
- Python Web Server Gateway Interface -- WSGI
了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的Body发送给 ...
- a simple and universal interface between web servers and web applications or frameworks: the Python Web Server Gateway Interface (WSGI).
WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server comm ...
- Python Web框架 bottle flask
Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 4 pip instal ...
- Python Web开发之Flask
PythonWEB框架之Flask 前言: Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenT ...
- python web server gateway interface (wsgi ) notes
前言: 注:如果需要得到支持批Python3.x以及包含了勘误表,附录,和说明的更新版规范,请查看PEP 3333 摘要: 这篇文档详细说明了一套在web服务器与Python web应用程序(web框 ...
- Python WEB框架之Flask
前言: Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenType....): Tornado: ...
- Notes on PEP333 (Python Web Server Gateway Interface)
This note is about PEP3333- Python Web Server Gateway Interface. Refer to (Source: http://legacy.pyt ...
- Python Web实战 - 基于Flask实现的黄金点游戏
一.简介 团队成员: 领航者:张旭 驾驶员:张国庆 项目简介: 项目名称:基于B/S模式的黄金点游戏 采用技术: 后端:Python + Sqlite3 前端:HTML + CSS + JS + Bo ...
随机推荐
- JIRA管理员、用户密码重置
-- Jira数据库中,用户信息都存放在表 cwd_user中 -- 切换到jiar数据库 use jiradb; -- 更改密码为sphere update cwd_user set credent ...
- selenium webdriver学习(八)------------如何操作select下拉框(转)
selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriv ...
- uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- H3C ARP
- Android本地数据存储: Reservoir
一:前言 今天做项目,准备使用本地存储,把一些数据存在本地磁盘上,比如用户名.密码这样的.其实大家都知道,这种情况最常用的就是SharedPreferences了,我也不例外,脑子里第一个想到的就是用 ...
- 2008年NOIP普及组复赛题解
题目涉及算法: ISBN号码:简单字符串模拟: 排座椅:贪心: 传球游戏:动态规划: 立体图:模拟. ISBN号码 题目链接:https://www.luogu.org/problem/P1055 简 ...
- H3C 帧中继与水平分割
- java基本数据类型转换之向上转型和向下转换
向上转换: 整型,字符型,浮点型的数据在混合运算中相互转换,转换时遵循以下原则: 容量小的类型可自动转换为容量大的数据类型: byte,short,char → int → long → float ...
- 如何查看linux中的ssh端口开启状态
netstat -anp |grep 22 netstat -anp |grep sshlsof -i :22
- springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)
一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...