当url是本地主机vhost时,为什么使用Python3 urllib.request.request会导致“由对等方重置连接”?

2024-09-30 00:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Devilbox进行本地的web开发工作(主要与WordPress相关)。我一直在开发一个Python3应用程序,它在Devilbox vhost WordPress安装中提交表单。我正在使用的这个Python脚本使我能够轻松快速地为插件/主题工作设置WordPress实例

我遇到的问题是,当我通过urllib.request.request进行连接时,我的结果是“ConnectionResetError:[Errno 104]由对等方重置连接”

以下是我用来连接的相关代码:

        wp_host = 'http://' + vhost_name + '.loc'

        values = {
            'dbname': vhost_name,
            'uname': 'root',
            'pwd': '',
            'dbhost': database_name + '.loc',
            'prefix': 'wp_'
        }

        install_step_two_url = wp_host + '/wp-admin/setup-config.php?step=2'
        print('install step two url: ' + install_step_two_url)

        req = urllib.request.Request(url=install_step_two_url,
            data=urllib.parse.urlencode(values).encode(),
            headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36','Content-Type': 'application/x-www-form-urlencoded'})
        response = urllib.request.urlopen(req)

我尝试在vhost域之后添加端口80,但没有成功。如果您能给我提供任何帮助或提示,为我指明正确的方向,我们将不胜感激


Tags: installnamehosturlrequeststepwordpressurllib
1条回答
网友
1楼 · 发布于 2024-09-30 00:22:49

让我们假设vhost_的名字是“foo”

when I connect ... [I get] ... "Connection reset by peer".

您没有运行侦听端口80请求的Web服务器

核实:

$ telnet foo.loc http

或:

$ netstat -an | grep 80; sudo lsof -i :80

您需要将vhost_名称指向 提供HTTP服务的有效主机

您的主机可能有两个IP, 比如10.2.3.4和127.0.0.1, 而您的Web服务器只对其中一个发出了bind()

相关问题 更多 >

    热门问题