質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.40%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

255閲覧

ローカルホストにFastAPIでサーバーを構築し、requestsでアクセスしようとしたらステータスが503になる

gymgym

総合スコア99

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2024/07/12 03:30

編集2024/07/12 04:29

実現したいこと

ローカルホストにFastAPIでサーバーを構築し、requestsでアクセスしたい

発生している問題・分からないこと

FastAPIでサーバーは構築できるが、requestsでアクセスしようとしたら、ステータスが503になる

curl "http://localhost:8080"ではうまく実行されます。
pythonでrequestsから実行すると530になってしまいます。

エラーメッセージ

error

1503 2<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 3<html><head> 4<meta type="copyright" content="Copyright (C) 1996-2016 The Squid Software Foundation and contributors"> 5<meta http-equiv="Content-Type" CONTENT="text/html; charset=utf-8"> 6<title>ERROR: The requested URL could not be retrieved</title> 7<style type="text/css"><!-- 8 /* 9 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors 10 * 11 * Squid software is distributed under GPLv2+ license and includes 12 * contributions from numerous individuals and organizations. 13 * Please see the COPYING and CONTRIBUTORS files for details. 14 */ 15 16/* 17 Stylesheet for Squid Error pages 18 Adapted from design by Free CSS Templates 19 http://www.freecsstemplates.org 20 Released for free under a Creative Commons Attribution 2.5 License 21*/ 22 23/* Page basics */ 24* { 25 font-family: verdana, sans-serif; 26} 27 28html body { 29 margin: 0; 30 padding: 0; 31 background: #efefef; 32 font-size: 12px; 33 color: #1e1e1e; 34} 35 36/* Page displayed title area */ 37#titles { 38 margin-left: 15px; 39 padding: 10px; 40 padding-left: 100px; 41 background: url('/squid-internal-static/icons/SN.png') no-repeat left; 42} 43 44/* initial title */ 45#titles h1 { 46 color: #000000; 47} 48#titles h2 { 49 color: #000000; 50} 51 52/* special event: FTP success page titles */ 53#titles ftpsuccess { 54 background-color:#00ff00; 55 width:100%; 56} 57 58/* Page displayed body content area */ 59#content { 60 padding: 10px; 61 background: #ffffff; 62} 63 64/* General text */ 65p { 66} 67 68/* error brief description */ 69#error p { 70} 71 72/* some data which may have caused the problem */ 73#data { 74} 75 76/* the error message received from the system or other software */ 77#sysmsg { 78} 79 80pre { 81 font-family:sans-serif; 82} 83 84/* special event: FTP / Gopher directory listing */ 85#dirmsg { 86 font-family: courier; 87 color: black; 88 font-size: 10pt; 89} 90#dirlisting { 91 margin-left: 2%; 92 margin-right: 2%; 93} 94#dirlisting tr.entry td.icon,td.filename,td.size,td.date { 95 border-bottom: groove; 96} 97#dirlisting td.size { 98 width: 50px; 99 text-align: right; 100 padding-right: 5px; 101} 102 103/* horizontal lines */ 104hr { 105 margin: 0; 106} 107 108/* page displayed footer area */ 109#footer { 110 font-size: 9px; 111 padding-left: 10px; 112} 113 114 115body 116:lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } 117:lang(he) { direction: rtl; } 118 --></style> 119</head><body id=ERR_CONNECT_FAIL> 120<div id="titles"> 121<h1>ERROR</h1> 122<h2>The requested URL could not be retrieved</h2> 123</div> 124<hr> 125 126<div id="content"> 127<p>The following error was encountered while trying to retrieve the URL: <a href="http://localhost:8080/">http://localhost:8080/</a></p> 128 129<blockquote id="error"> 130<p><b>Connection to 127.0.0.1 failed.</b></p> 131</blockquote> 132 133<p id="sysmsg">The system returned: <i>(111) Connection refused</i></p> 134 135<p>The remote host or network may be down. Please try the request again.</p> 136 137<br> 138</div> 139 140<hr> 141<div id="footer"> 142<p>Generated Fri, 12 Jul 2024 04:24:54 GMT by unknown (squid/3.5.20)</p> 143<!-- ERR_CONNECT_FAIL --> 144</div> 145</body></html> 146 147### 該当のソースコード 148 149```python 150# main.py 151from fastapi import FastAPI 152from fastapi.middleware.cors import CORSMiddleware 153import uvicorn 154 155app = FastAPI() 156 157@app.get("/") 158async def index(): 159 return {"message": 'Hello World!'} 160 161 162if __name__=="__main__": 163 uvicorn.run("main:app",host='localhost', port=8080, reload=True)

python

1#request_test.py 2import requests 3 4proxies = { 5 'http': 'http://proxy', 6 'https': 'http://proxy', 7} 8#res = requests.get("http://localhost:8080", proxies=proxies) 9res = requests.get("http://localhost:8080") 10 11print(res.status_code) 12print(res.text)

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

ローカルホストなので関係ないかもしれませんが、プロキシ設定の設定やファイアウォールの設定でポートへの接続を許可したりしてみました。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TakaiY

2024/07/12 04:09 編集

requestsにproxyの設定を入れているのはなぜですか? proxyの設定無しで実行するとどのようになりますか? 回答はここではなく、質問を編集して追記ください。
gymgym

2024/07/12 04:30

コメントありがとうございます。 最初はproxy設定なしで実行していましたが、503でエラーが発生し、試しにプロキシ設定を入れてみて実行しました。
TakaiY

2024/07/12 05:22

応答メッセージにある squid は、プロキシサーバの実装の1つなので、プロキシを経由しようとしてしまっているのが原因です。 ただし、指定を外してもそうなるのはなぜでしょうね。 ・curlを実行しているPCと、pythonを実行しているPCは同じですか? ・環境変数でPROXYを指定していたりしませんか?
TakaiY

2024/07/12 05:23

(同様の内容の回答ついてますね。 そちらに任せます)
guest

回答1

0

ベストアンサー

レスポンスの143行目

<p>Generated Fri, 12 Jul 2024 03:20:42 GMT by unknown (squid/3.5.20)</p>

を読めばsquidが生成しているレスポンスだと分かります
squidが超有名プロクシソフトウェアだと知っていれば、プロクシに問い合わせされていてエラーになったと判断できます

localhostにアクセスするのにプロクシは必要ないです

python

1res = requests.get("http://localhost:8080")

でよいです

プロクシが必要なのだったら、アクセス先がlocalhostのはずはないです


状況は変わりありません
日時が違うだけでエラーを返しているのはsquidですから、なぜプロクシを経由しているのか? に謎が変わっただけです
HTTP_PROXY環境変数などが設定されていませんか?

そうならHTTP_PROXY環境変数を消してチャレンジするか、NO_PROXY=localhost を設定するとかです


python

1proxies = { 2 'http': 'http://proxy', 3 'https': 'http://proxy', 4 'no_proxy': 'localhost,127.0.0.1', 5} 6res = requests.get("http://localhost:8080", proxies=proxies)

でもよいかもしれません

https://github.com/psf/requests/blob/v2.32.3/tests/test_utils.py#L235
https://github.com/psf/requests/blob/v2.32.3/src/requests/utils.py#L878

投稿2024/07/12 04:19

編集2024/07/12 05:11
quickquip

総合スコア11165

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gymgym

2024/07/12 04:32

ご回答ありがとうございます。 最初はプロキシ設定入れていませんでしたが、うまくいかなかったので試しに入れてみただけになります。 プロキシ設定なしでのエラー文に更新させていただきました。
gymgym

2024/07/12 06:20

回答ありがとうございます。 試したところ問題が解決しました! ベストアンサーに選ばせていただきました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.40%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問