【问题标题】:Two-way communication between Angular (version 6 using HttpClient) and PythonAngular(使用 HttpClient 的版本 6)和 Python 之间的双向通信
【发布时间】:2018-07-17 11:43:32
【问题描述】:

我非常熟悉 Angular 的前端,它是新的 HttpClient。 我从未处理过后端,尤其是用 Python 编写的后端(这就是为什么我发现以下任务非常具有挑战性)。 我想要完成的只是 Angular 6 和 Python 之间的双向通信(Python 中的代码将字符串发送到 Angular 的 Typescript 中的代码,反之亦然 - 将字符串发送到用 Python 编写的代码的 typeScript 代码)。

在网络上找不到任何教程(此处为堆栈溢出/msdn)。 Python中的后端是由另一家公司开发的,他们的要求是这样的:

Python 代码应该是 3-4 行,如

Import some_module
Socket(..)/pipe()
Send(some_string)/receive(some_string)

谁能帮我准备一些如何完成的示例代码?

我猜在 Angular 方面,我应该只发出一个 HTTP 请求,通过 API 向 Python 发送一个字符串:

const observable = this.http.post<string>(AppConfig.baseUrl + sendToPythonUrl + stringToSend, {}).pipe(share());
    return observable

Python 是如何接收这个字符串的?

另外,Python 如何与 Angular 服务通信并向其发送字符串?

【问题讨论】:

  • 你在使用 socket.io 吗?
  • 不,我去看看。中间用C#可能对我更好?

标签: python angular httpclient


【解决方案1】:

像这样使用 HttpClient:

constructor(private httpClient: HttpClient) {
    }


/**
     * This method is use for send GET http Request to API.
     * @param url - Additional request URL.
     * @param body - params.
     * @param options  - Header(s) which will pass with particular request.
     */
    get(url: string, options?: any): Observable<any> {

        return this.httpClient.get(url, {options})
    }

component.ts 中的订阅数据:

constructor(
        private service: ServiceClass){}



getData(){
     this.service.get('url',this.options).subscribe(resp => {

console.log(resp);
   })
   }

【讨论】:

  • 好的,我知道如何通过 Angular 的 HttpClient 发布或获取信息。我感兴趣的是它是如何在 Python 的后端完成的,以及如何通过示例代码对其进行测试。
  • 你需要了解python中的api开发(如django)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 2014-05-24
相关资源
最近更新 更多