赞
踩
Python的服务端调用代码如下所示:
from flask import Flask, request
class AiServer(object):
def __init__(self):
server = Flask(__name__)
server.route('/post', methods=['POST'])(self.post)
server.run(host="0.0.0.0", port=8000, threaded=True)
def post(self):
if request.method == "POST":
print("get post")
response_data = request.json.get('data')
print("response_data", response_data)
return "Hello"
if __name__ == '__main__':
AiServer()
Python客户端实现如下所示:
import requests
if __name__ == '__main__':
# post 请求
url = "http://localhost:8000/post"
data = "num"
r = requests.post(url, json={"data": data})
print(r.text)
r = requests.post(url, json={"data": data})
print(r.text)
C#客户端实现如下所示:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace SabberStoneBasicAI
{
internal class Program
{
private static readonly Random Rnd = new Random();
private static void Main1()
{
Console.WriteLine("Starting test setup.");
string url = "http://localhost:8000/post";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
//request.Timeout = "800"; // 设置请求超时,单位毫秒。
request.ContentType = "application/json";
string postData = "{\"data\":\"value1\"}";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
// 定义请求流
Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(data, 0, data.Length);
myRequestStream.Close();
// 发送
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取返回值
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
Console.WriteLine(retString);
Console.WriteLine("Test ended!");
Console.ReadLine();
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。