Uncategorized

ip – How can I connect from a global network to a local python computer?


How can I connect from the client computer to the server computer?
enter image description here
If you access the local network at the ip address 192.168.0.7, then everything works, but if via the global ip 93.185.199.xx, the TimeoutError error pops up: [WinError 10060] The attempt to establish a connection was unsuccessful, because the desired response was not received from another computer in the required time, or the already established connection was terminated due to an incorrect response an already connected computer.
client.py.

import socket
client = socket.socket() # creating a client socket
hostname = "93.185.199.36" # get the host of the local machine
port = 5405 # setting the server port
client.connect((hostname, port)) # connecting to the server
message = input("Input a text:") # enter the message
client.send(message.encode()) # send the message to the server
data = client.recv(1024) # getting data from the server
print("Server sent: ", data.decode())
client.close() # closing the connection

server.py

import socket
server = socket.socket()
hostname="0.0.0.0"
print(hostname)
port = 5405 # setting the server port

server.bind((hostname, port)) # bind the server socket to the server host and port
server.listen(4) # start listening for incoming connections
print("Server running")
while True:
con, _ = server.accept() # accept the client
data = con.recv(1024) # receive data from the client
message = data.decode() # convert bytes to string
print(f"Client sent: {message}")
message = message[::-1] # invert the string
con.send(message.encode()) # sending a message to the client
con.close() # closing the connection

I have created a “virtual server” in the router for forwarding via port 5405
enter image description here
I also opened port 5405 on the PC server
enter image description here
Disabled the microsoft defender
enter image description here



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *