S
S
swyt2022-01-05 09:26:21
Python
swyt, 2022-01-05 09:26:21

Why does not connect when opening ports?

I have a program that should connect to the server, if connected via local network, then it connects, if via external ip, then there is no connection. I open the port using NAT-PMP, it also makes a torrent, but for some reason it doesn’t work for me. What could be the problem?

Server:

import socket

listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('localhost', 9999))
listener.listen(0)
print("Wait")
listener.accept()
print("Connect")

Customer:
import socket

connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(('external_ip', 9999))
connection.close()

Port opening:
import natpmp

natpmp.map_tcp_port(9999, 9999)
natpmp.map_udp_port(9999, 9999)

61d539ed26250299217361.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2022-01-05
@Adler_lug

Although not quite in the subject, but these two lines confuse me:

listener.bind((' localhost ', 9999))
and
connection.connect((' external_ip ', 9999))

Bind localhost, which, in theory, has nothing to do with opening ports, but you are trying to connect in an external IP.

A
alekssamos, 2022-01-05
@alekssamos

changed localhost to 0.0.0.0 and everything works.

Using username "root".
Authenticating with public key "imported-openssh-key"
Linux v43195 4.9.0-16-amd64 #1 SMP Debian 4.9.272-2 (2021-07-19) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
[email protected]:~# python3
Python 3.7.2 (default, Oct 10 2020, 15:44:37)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>>
>>> listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> listener.bind(('0.0.0.0', 9999))
>>> listener.listen(0)
>>> print("Wait")
Wait
>>> listener.accept()

(<socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('46.17.45.71', 9999), raddr=('5.101.156.100', 56353)>, ('5.101.156.100', 56353))
>>> print("Connect")
Connect
>>>

Using username "x90048bt".
Pre-authentication banner message from server:
| Welcome to LTD BeGet SSH Server 'bigbone'
End of banner message from server
Authenticating with public key "imported-openssh-key"
Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 4.9.207-1-beget-acl x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Sun Jan  2 18:58:44 2022 from 85.249.21.110
[email protected]:~ [0] $ python
Python 2.7.3 (default, Oct 26 2016, 21:01:49)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> connection.connect(('46.17.45.71', 9999))
>>> connection.close()
>>>

E
Eugene Moldovanu, 2022-01-06
@Devvver

One NAT forwarding is not enough, you also need the provider to allow it.
Programs that use the network behind the nat use STUN servers.
Torrent programs (bitorrent, utorrent) also use intermediaries to calculate the external port from the NAT provider.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question