• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Flask使用------python算法后端连接c#Winform前端

武飞扬头像
小小小姝
帮助1

Flask使用------python算法后端连接c#Winform前端

前段时间,一直在纠结怎么将Ubuntu系统的python算法和Windows系统的C#连接起来,后面在经过搜了很多资料后,发现了Flask这个神奇的东西。学了几天,做出了自己需要的东西,就没有继续深入学习下去了,毕竟现在没有很多时间去学习。等暑期可以深入学一下,还挺有意思的。

我在网上搜集了一些有关python和C#用Flask连接的例子,但是没有我想要的,索性自己边看边捣鼓也差不多捣鼓出来了。就把代码贴到这里了。

前提:我的python算法需要传出的是图片,其传入的有时是图片,有时是string(这里只放了前端代码,后端代码在之前的博客里面贴出来啦!)

前端代码:
 		/// <summary>
        ///  通过网络地址和端口访问数据:image型--image型
        /// </summary>
        /// <param name="Url">网络地址</param>
        /// <param name="jsonParas">json参数</param>
        /// <returns></returns>
        public Image RequestsPostImage(string Url, byte[] jsonParas)
        {
            string postContent = "";
            string strUrl = Url;
            Image img = null;

            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            //Post请求方式
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";

            //设置参数,并进行URL编码
            byte[] payLoad = jsonParas;
  
            //设置请求的ContentLength
            request.ContentLength = payLoad.Length;

            //发送请求
            Stream writer;
            try
            {
                //获取用于写入请求数据的Stream对象
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
                MessageBox.Show("连接服务器失败");
                return null;
            }
            //将请求参数写入流
            writer.Write(payLoad, 0, payLoad.Length);
            //关闭请求流
            writer.Close();

            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                postContent = "default: The response is null."   "Exception: "   ex.Message;
                MessageBox.Show(postContent);
            }
            if (response != null)
            {
                try
                {
                    Stream s = response.GetResponseStream();
                    img = Image.FromStream(s);
                }
                catch (Exception e)
                {
                    postContent = "default: The data stream is not readable."   "\r\n"   e.Message;
                    MessageBox.Show(postContent);
                }
            }
            //返回JSON数据
            return img;
        }

        /// <summary>
        ///  通过网络地址和端口访问数据:string型--image型
        /// </summary>
        /// <param name="Url">网络地址</param>
        /// <param name="jsonParas">json参数</param>
        /// <returns></returns>
        public Image RequestsPostString(string Url, string jsonParas)
        {
            string postContent = "";
            string strUrl = Url;
            Image img = null;

            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            //Post请求方式
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/json";

            //设置参数,并进行URL编码
            string paraUrlCoded = jsonParas;
            byte[] payLoad;
            //将json字符串转化为字节
            payLoad = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength
            request.ContentLength = payLoad.Length;

            //发送请求
            Stream writer;
            try
            {
                //获取用于写入请求数据的Stream对象
                writer = request.GetRequestStream();
            }
            catch (Exception)
            {
                writer = null;
                MessageBox.Show("连接服务器失败");
                return null;
            }
            //将请求参数写入流
            writer.Write(payLoad, 0, payLoad.Length);
            //关闭请求流
            writer.Close();

            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
                postContent = "default: The response is null."   "Exception: "   ex.Message;
                MessageBox.Show(postContent);
            }
            if (response != null)
            {
                try
                {
                    Stream s = response.GetResponseStream();
                    img = Image.FromStream(s);
                }
                catch (Exception e)
                {
                    postContent = "default: The data stream is not readable."   "\r\n"   e.Message;
                    MessageBox.Show(postContent);
                }
            }
            //返回JSON数据
            return img;
        }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgahbih
系列文章
更多 icon
同类精品
更多 icon
继续加载