简体   繁体   English

如何从远程连接访问python http服务器?

[英]How do I access a python http server from a remote connection?

I'm trying out the command python http.server from the command line, and am trying to access the server. 我正在从命令行尝试命令python http.server,我正在尝试访问服务器。 I can access easily from localhost, but whenever I try to use a remote connection, I am unable to connect. 我可以从localhost轻松访问,但每当我尝试使用远程连接时,我都无法连接。

I've tried different ports, and it doesn't look like my firewall is blocking any connections. 我尝试过不同的端口,看起来我的防火墙不会阻止任何连接。

From the command line, I run 从命令行,我运行

python3 -m http.server 8000

which returns 返回

Serving HTTP on 0.0.0.0 port 8000 ...

However, I can only connect to the server from localhost. 但是,我只能从localhost连接到服务器。

sudo iptables -S

returns the following: 返回以下内容:

-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

I expect to be able to connect from a remote host with the url http://10.247.30.125:8000 (my ip address), but I am unable to do so. 我希望能够通过网址http://10.247.30.125:8000 (我的IP地址)从远程主机连接,但我无法这样做。 The same url works fine on localhost. 同一个url在localhost上运行正常。

url中缺少端口尝试此URL它应该工作“ http://10.247.30.125:8000

I figured out what was going on. 我弄清楚发生了什么。 Did some reading about IP and NAT, and noticed that any ip address beginning with a 10 is reserved for private ip addresses. 做了一些关于IP和NAT的阅读,并注意到任何以10开头的ip地址都是为私有IP地址保留的。 So, trying to connect to 10.247.30.125:8000 from anywhere other than my own network (so while not connected to my wifi) doesn't work. 所以,尝试从我自己的网络以外的任何地方连接到10.247.30.125:8000(所以虽然没有连接到我的wifi)不起作用。 To be able to connect from an external network, I would need to set up port forwarding (like Reedinationer suggested), and I would need to use my router's public IP address rather than my computer's private IP address. 为了能够从外部网络连接,我需要设置端口转发(如建议的Reedinationer),我需要使用我的路由器的公共IP地址而不是我的计算机的私有IP地址。 I would set up a port forward from my router to direct external traffic to my computer, which would allow me to connect to my personal computer from an external network. 我会从路由器向前设置一个端口,将外部流量引导到我的计算机,这样我就可以从外部网络连接到我的个人计算机。

Thanks to everyone who responded! 感谢大家的回应!

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

相关问题 Python3:如何关闭由http.server打开的连接? - Python3:How do I close connection opened by http.server? 如何使用Python通过HTTP读取远程Zip存档中的选定文件? - How do I read selected files from a remote Zip archive over HTTP using Python? python:从http服务器下载文件时,远程主机正在关闭连接 - python : Remote host is closing connection while downloading files from http server 如何从Windows系统将文件从python脚本复制到任何其他远程服务器? - How do I copy files from windows system to any other remote server from python script? 如何为远程 Python 结构访问获取正确的 $PATH? - How do I get the correct $PATHs for remote Python Fabric Access? 如何在Windows上使用Python访问远程文件系统? - How do I access a remote filesystem using Python on Windows? 如何使用python检查远程服务器上是否存在文件? - How do i check if a file exists on remote server using python? 如何正确地将 Python 中的 BCP 命令发送到远程服务器? - How do I properly send a BCP command in Python to a remote server? 如果停止,如何在远程服务器上重新启动python脚本? - How do I restart a python script on a remote server if it stops? 如何在 Python 中使用 SSH 连接从远程主机发送 HTTP GET 请求? - How to send HTTP GET request from remote host using SSH connection in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM