简体   繁体   English

从 Linux 上的 Python 脚本访问 Popen web 服务器

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

I have this script and try to run it on CentOS 7 (though I'm fairly sure the distro doesn't matter too much):我有这个脚本并尝试在 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()

I know the Popen is correct in principle, because if I run this script instead, I can read from the server with something like curl localhost:8000 :我知道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()

I also know the .urlopen() command should work, since I can run a similar web server from the command line, then open an interactive Python session and run the same two lines:我也知道.urlopen()命令应该可以工作,因为我可以从命令行运行类似的 web 服务器,然后打开交互式 Python Z21D6F40CFB511982E4424E0E250A955Z 并运行相同的两行:

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

And it will print the expected page.它将打印预期的页面。

My question: why would running the first script result in connection errors?我的问题:为什么运行第一个脚本会导致连接错误?

Specifically, I get these errors:具体来说,我收到这些错误:

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>

I feel like I'm missing something obvious, but am not seeing it.我觉得我错过了一些明显的东西,但没有看到它。 More so because the exact same script runs with expected results on Windows.更是如此,因为完全相同的脚本在 Windows 上运行并获得预期结果。

Note that I'm not planning on running anything important with the above code, I just had something like this sit in an old unit test somewhere and cannot figure out why it won't work on this platform.请注意,我不打算使用上述代码运行任何重要的东西,我只是在某处的旧单元测试中放置了类似的东西,无法弄清楚为什么它不能在这个平台上运行。

You'll need to wait.你需要等待。 You have no guarantee that the web server has initialized itself and is ready to serve requests by the time you issue your request.您无法保证 web 服务器已自行初始化并准备好在您发出请求时为请求提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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