简体   繁体   中英

Two-way communication between Angular (version 6 using HttpClient) and Python

I'm familiar very well with the Front-End of Angular and it's new HttpClient. I've never dealt with the back-end, and especially not the one written in Python (that's why I find the following task a quite challenging). All that I'm trying to accomplish is a two way communication between Angular 6 and Python (a code in Python that sends a string to a code in Angular's Typescript and vice versa- A typeScript code that sends a string to a code written in Python).

Could not find any tutorial on the web (here in stack overflow / msdn). The back-end in Python is developed by a another company, and their requirement is something like:

The Python code should be 3-4 lines like

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

Can anyone please help me to prepare some sample code of how it can be done?

I guess that on the Angular side, I should just make an HTTP request to send a string to Python via API:

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

How does Python receive this string?

Also, how Python can communicate with the Angular service and send a string to it?

Use HttpClient something like this:

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})
    }

Subscribedata in component.ts:

constructor(
        private service: ServiceClass){}



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

console.log(resp);
   })
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM