簡體   English   中英

Python:即使是無效的 url 也返回 200 狀態碼

[英]Python: Return 200 status code even for invalid urls

所以我想說的是,我想將 get/post 請求發送到無效的 url(例如https://this-is-a-fake-url.com )我知道它會出錯,因為 url 不存在但我想知道一種方法,以便它給出 200 響應代碼。 這樣如果有人使用 wireshark 或其他東西捕獲 api 請求,他會看到許多請求都返回代碼 200,無論鏈接是否有效。 有可能嗎? 如果是這樣,請幫助:)

您可以啟動本地服務器,並將其添加到hosts文件中。 更具體地說,如果您使用的是 linux 機器:

  1. 運行以下命令,它會將this-is-a-fake-url.com為您的本地主機
sudo cat '127.0.0.1 this-is-a-fake-url.com' >> /etc/hosts
# In windows, write to C:\Windows\System32\drivers\etc
  1. 在本地主機上啟動服務器
sudo python3 -m http.server
# Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
  1. 訪問你的假主機
curl http://this-is-a-fake-url.com:8000 -v
# *   Trying 127.0.0.1:8000...
# * TCP_NODELAY set
# * Connected to this-is-a-fake-url.com (127.0.0.1) port 8000 (#0)
# > GET / HTTP/1.1
# > Host: this-is-a-fake-url.com:8000
# > User-Agent: curl/7.68.0
# > Accept: */*
# > 
# * Mark bundle as not supporting multiuse
# * HTTP 1.0, assume close after body
# < HTTP/1.0 200 OK
# ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM