0

I've been recently introduced to the standard socket module in python, and have begun experimenting with it myself. However, upon sending my projects to friends, I've soon bumped into the problem that the programs have only local network reach.

Here's an example of what I've been working on- it's a simple program that connects a server to one client, then engages them in a simple chat loop indefinetly. This program has been copied from this video tutorial, with some modifications: https://www.youtube.com/watch?v=YwWfKitB8aA&t=1614s

(IP has been slightly modified for privacy purposes.) SERVER:

import socket

HOST = '192.168.0.1'
PORT = 43218

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))

server.listen()
commsocket, address = server.accept()
print(f"Connected to {address}")

while(True):
    message = commsocket.recv(1024).decode("utf-8")
    print(f"Message from client: {message}")
    returnmessage = input("Type in message to send: ")
    commsocket.send(returnmessage.encode('utf-8'))


CLIENT:

import socket
from time import sleep
HOST = '192.168.0.1'
PORT = 43218

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.connect((HOST, PORT))

while(True):
    message = input("Type in message to send, then press enter: ")
    server.send(message.encode('utf-8'))
    print(f"Message from server {server.recv(1024).decode('utf-8')}")

Is it in any way possible to modify this so that anyone from outside my LAN (if possible, even to a global reach) can use this program as a client? If possible, with only the use of the vanilla socket module.

Thank you

  • Does this answer your question? [How do I make a TCP server work behind a router (NAT) without any redirection configuration needed](https://stackoverflow.com/questions/1511562/how-do-i-make-a-tcp-server-work-behind-a-router-nat-without-any-redirection-co) – Homer512 Nov 19 '22 at 11:44

0 Answers0