简体   繁体   中英

How to communicate between two machines with public IP but both behind LAN(python solution)?

Looking for a simple python solution for this:

Assume Machine A's local IP is 192.168.1.5 and public IP is 111.11.111.11, Machine B's local IP is 10.0.0.5 and public IP is 222.22.222.22, now I need to communicate between these two machines, how do you do that?

I've seen lots of socket programming examples with both server and client on the same network, but how do you connect machines behind different networks without changing router configurations(port forwarding etc.)?

EDIT:

What if Machine A's behind LAN: local IP 192.168.1.5, public IP 111.11.111.11; Machine B's an HTTP server with a public IP of 222.22.222.22; now you can easily reach B from A through HTTP requests, but what's the best solution to reach machine A from machine B?

Hole Punching is one way to achieve this. Hole punching is a NAT traversal technique, which allows direct communication between devices behind NATs. Not all NATs support hole punching. But a good percentage of NATs allow.

http://www.brynosaurus.com/pub/net/p2pnat/

This link explains it in detail. They have given the statistics also.

For the first case, when both the devices are behind NAT, you need to either do hole punching or use a UPnP python library to forward ports if you don't want to forward them manually.

For the second case, the program on machine A must initialize the connection. The NAT on this end will create a translation entry in it's table. Basically, any packets sent from machine B on your network's public IP address will now be forwarded to machine As local IP address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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