What's Up?

OK, So I have a working python script that sends and receives text to and from the client and server python files, However if the Server and Client are on different computers, And therefore we need to connect over the internet, How would we do that?

New to python, Go easy on me :-O

See Ya!

Dani AI

Generated

already has a working local client/server setup. Making that pair talk over the internet requires three things: the server must listen on an interface reachable from outside the host, the network path (router + firewall) must allow incoming connections on the chosen port, and the remote client must use the server's public address (or a reachable hostname). was pointing in the right direction; the steps below make that concrete and add common gotchas.

A short checklist:

  • Bind the server socket to 0.0.0.0 (or the host IP) so it accepts remote connections, not just localhost.
  • Use a port above 1024 unless running as root. Reserve and document the port.
  • If the server is behind a home/office router, add a port-forward mapping from the router's public port to the server machine's internal IP/port.
  • Open the server port in the host firewall (Windows Firewall, ufw, iptables, etc.).
  • If the public IP is dynamic, use a dynamic-DNS name or a tunnel service for reliable addressing.

Minimal Python examples (TCP):

# server.py
import socket
s = socket.socket()
s.bind(('0.0.0.0', 5000))
s.listen(1)
conn, addr = s.accept()
data = conn.recv(1024)
conn.sendall(b'OK')
conn.close()
# client.py
import socket
s = socket.socket()
s.connect(('PUBLIC_IP_OR_HOSTNAME', 5000))
s.sendall(b'hello')
print(s.recv(1024))
s.close()

Quick testing and security notes: verify reachability from outside using telnet PUBLIC_IP 5000 or nc -vz PUBLIC_IP 5000. If router/NAT blocks incoming ports or carrier NAT is used, use a tunneling service such as ngrok for quick testing. Do not expose plain sockets to the internet without authentication or encryption; wrap sockets with TLS (ssl module) or use an SSH tunnel for sensitive data. For reference on socket usage see the Python socket docs: Python socket docs.

Recommended Answers

All 2 Replies

When you say you are currently sending code between client and server on the same machine, how are you doing this? Via a local socket or what?

If so, you just need to give the client the IP address/port of the server and connect via the "connect" method of your socket object and you will be good to go.

If not, just post back and I'll try to help you out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.