0
I’m trying to chat at python
, however server
only connects with the client.py
if the client’s machine is connected on the same network. How can I make the two connect to different networks (remotely)? Follow the code server.py
:
import socket
import subprocess
import threading
#IP = "localhost"
PORT = 4444
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', PORT)) #server.bind((IP, PORT))
server.listen(1)
print("[+] Serv startado")
client, client_addr = server.accept()
print(f" {client_addr} O cliente abriu o chat")
def enviar():
global client, client_addr
while True:
msg = input("MSG>>> ")
msg = msg.encode()
client.send(msg)
print("Mensagem enviada!")
def receber():
global client, server
while True:
msg2 = client.recv(1024)
msg2 = msg2.decode(encoding = 'UTF-8',errors = 'ignore')
print("\nOutput: ", msg2)
threading.Thread(target=enviar).start()
threading.Thread(target=receber).start()
And now, client.py:
import socket
import subprocess
import threading
SERV_IP = socket.gethostname()
#SERV_IP = "192.168.1.105"
SERV_PORT = 4444
back = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
back.connect((SERV_IP, SERV_PORT))
def receber():
global back
while True:
msg = back.recv(1024)
msg = msg.decode() #encoding = 'UTF-8',errors = 'ignore'
print("\nOutput: ", msg)
def enviar():
global back
while True:
msg2 = input("MSG>>> ")
msg2 = msg2.encode()
back.send(msg2)
print("Mensagem enviada!")
threading.Thread(target=receber).start()
threading.Thread(target=enviar).start()
Des de ja, thank you!