简体   繁体   English

Python套接字不接受外部连接

[英]Python socket not accepting external connections

I have a python socket that is supposed to be listening to all incoming sockets on port 2022 and I have port forwarding enabled as well as a dynamic DNS service. 我有一个python套接字,应该正在侦听端口2022上的所有传入套接字,并且启用了端口转发以及动态DNS服务。 When I am connected to the same network and I try to connect to my pc using the pc's IP address it works like a charm but when I try to use the public IP of my network it won't work although I have done proper port forwarding. 当我连接到同一网络并且尝试使用PC的IP地址连接到我的PC时,它就像一个超级按钮一样工作,但是当我尝试使用我的网络的公共IP时,尽管我做了正确的端口转发,但是它无法工作。 Here is my code, it stucks on the last line and doesn't accept any connections outside the network: 这是我的代码,它停留在最后一行,并且不接受网络外部的任何连接:

  IPC= '0.0.0.0'
  PORTC =2022
  client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client_socket.bind((IPC,PORTC))
  client_socket.listen(5)
  connection =client_socket.accept()[0].makefile('wb')

If you can connect from a different PC inside your private network, then there's nothing wrong with your Python code. 如果您可以从专用网络中的另一台PC进行连接,那么Python代码没有任何问题。 It's almost certain that there's something wrong with your router or the port forwarding config. 几乎可以肯定,您的路由器或端口转发配置有问题。

Make sure that you've re-reviewed your port forwarding settings and perhaps try restarting the router (be careful to verify your internally assigned IP addresses haven't changed if you go this route). 确保您已重新查看了端口转发设置,并可能尝试重新启动路由器(如果使用此路由,请务必确认内部分配的IP地址没有更改)。

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

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