簡體   English   中英

從 Linux 上的 Python 腳本訪問 Popen web 服務器

[英]Accessing Popen web server from Python script on Linux

我有這個腳本並嘗試在 CentOS 7 上運行它(盡管我相當確定發行版並不重要):

from subprocess import Popen, DEVNULL
from urllib import request

p = Popen(['python', '-m', 'http.server'], cwd='.', stderr=DEVNULL, stdout=DEVNULL)
try:
   r = request.urlopen('http://localhost:8000')
   print(r.read())
finally:
   p.terminate()
   p.wait()

我知道Popen原則上是正確的,因為如果我改為運行此腳本,我可以從服務器讀取curl localhost:8000之類的內容:

from subprocess import Popen, DEVNULL
from urllib import request
from time import sleep

p = Popen(['python', '-m', 'http.server'], cwd='.', stderr=DEVNULL, stdout=DEVNULL)
try:
   sleep(30)
finally:
   p.terminate()
   p.wait()

我也知道.urlopen()命令應該可以工作,因為我可以從命令行運行類似的 web 服務器,然后打開交互式 Python Z21D6F40CFB511982E4424E0E250A955Z 並運行相同的兩行:

r = request.urlopen('http://localhost:8000')
print(r.read())

它將打印預期的頁面。

我的問題:為什么運行第一個腳本會導致連接錯誤?

具體來說,我收到這些錯誤:

Traceback (most recent call last):
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 1342, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 950, in send
    self.connect()
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/http/client.py", line 921, in connect
    self.sock = self._create_connection(
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jaap.vandervelde/sandbox/run.py", line 6, in <module>
    r = request.urlopen('http://localhost:8000')
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 1371, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/home/jaap.vandervelde/.pyenv/versions/3.9.1/lib/python3.9/urllib/request.py", line 1345, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>

我覺得我錯過了一些明顯的東西,但沒有看到它。 更是如此,因為完全相同的腳本在 Windows 上運行並獲得預期結果。

請注意,我不打算使用上述代碼運行任何重要的東西,我只是在某處的舊單元測試中放置了類似的東西,無法弄清楚為什么它不能在這個平台上運行。

你需要等待。 您無法保證 web 服務器已自行初始化並准備好在您發出請求時為請求提供服務。

暫無
暫無

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

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