如何从docker容器内部发送发布请求?

时间:2019-05-02 14:40:41

标签: python docker

我正在尝试从服务器的docker容器内部发送发布请求。容器运行良好,直到尝试发出发布请求为止,此时挂起。

服务器具有Internet连接,该连接已通过ping命令进行了测试。

这是挂起的代码

import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Tweet} from '../api/models/response/Tweet';
import {Injectable} from '@angular/core';
import {environment} from '../../environments/environment';
import {AuthenticationService} from '../api/services/authentication.service';

@Injectable({
  providedIn: 'root'
})
export class TweetsService {
  tweetsUrl = '/tweets/timeline?page=0&size=25';
  sendTweetUrl = '/tweets/create';

  constructor(private httpClient: HttpClient,
              private authenticationService: AuthenticationService) {

  }

  getTweets(): Observable<Tweet[]> {
    return this.httpClient.get<Tweet[]>(environment.resourceApiUrl + this.tweetsUrl);
  }

  sendTweet(tweet: Tweet): Observable<Tweet> {
    return this.httpClient.post<Tweet>(environment.resourceApiUrl + this.sendTweetUrl, tweet);
  }
}

在“尝试发送帖子”之后,我看不到任何后面的打印语句,强制退出程序时收到的错误是:

while not send_flag:
        print('Attempting to send post')
        r = requests.post(
            'https://notarealaddress.fake', 
            headers = header,
            data = json.dumps(payload))
        if r.json()["status"] == 'success':
            print('Successfully Pushed')
            send_flag = True
        else:
            print('Failed to send alert with response:\n {}'.format(r.json()))
            sleep(60)

从本地计算机运行时,Docker映像成功运行,因此我发现很难发现问题。

1 个答案:

答案 0 :(得分:-1)

无论运行什么映像,都需要首先按照以下方法检查容器的IP和域名连接。

您可以在下面替换您的域名。

sudo docker run -it python ping www.google.com
PING www.google.com (172.217.160.68) 56(84) bytes of data.
64 bytes from tsa01s09-in-f4.1e100.net (172.217.160.68): icmp_seq=1 ttl=54 time=2.53 ms
64 bytes from tsa01s09-in-f4.1e100.net (172.217.160.68): icmp_seq=2 ttl=54 time=2.59 ms
64 bytes from tsa01s09-in-f4.1e100.net (172.217.160.68): icmp_seq=3 ttl=54 time=2.65 ms
64 bytes from tsa01s09-in-f4.1e100.net (172.217.160.68): icmp_seq=4 ttl=54 time=2.59 ms