My port forward rule
I've created a python TCP server and client which is working fine when I launch both server and client on my computer and when I launch the server and the client on different computers on the same network, however I wanted to make it work when computers are in different networks. I have forwarded my router port 8080 to convert to 8888 in my computer, in fact I have also a rule for the port 80 on my router converting to 8080 on my PC which is the Wamp server, and as the python server is not working I guessed it was from the code but I can't figure it out:
Server:
import socket
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.bind(("127.0.0.1", 8888))
sck.listen(1)
conn, adr = sck.accept()
print('Connected to ', adr)
while 1:
data = conn.recv(1024).decode()
if data and ('over' not in data):
conn.send(data.encode())
continue
break
conn.close()
Client:
import socket
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect(('<my_external_ip>', 8888))
while True:
data = input('Say something: ')
if 'Shut up' in data:
sck.send('over'.encode())
sck.close
break
sck.send(data.encode())
I've made this test:
Started Wamp server and tried to access it by my external ip with chrome (working)
Opened my Python TCP Client and connected to the Wamp server (working)
Turned Wamp off and started my python server with the same port as Wamp, then tried to acess with the python client (not working)
If there's nothing wrong with the TCP Server code why does Wamp work and he don't? Please check the following - Whats the logic behind binding 127.0.0.1 I couldn't understand... Won't that make it only be accessible by my PC?
If that part is ok than at least is something related to the Server code I guess...
Related
I'm experimenting with socket server-client communication using python 3 socket module. Just to test if an eventual connection can be made, I've tried to program the simplest scenario possible. Thus the server code has the only purpose to print new connections; its code is:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 30000))
while True:
server.listen()
print("listening...")
conn, addr = server.accept()
print(f"{addr} connected")
while the client only tries to connect to the server using the public IPv4 address of my computer:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Trying to connect...")
client.connect(("99.99.99.99", 30000))
client.close()
When I try to run the server and then the client nothing happens: the server just waits for conections, while the client displays nothing. After a while on the client side it is displayed the socket timeout error. I've tried to establish the connection even running the server and the client on the same computer, I've tried changing the ip of the server to "localhost" or "127.0.0.1", but nothing happens. Have you got any ideas on how this issue can be solved?
P.S. Thank you in advance for eventual answers and excuse my poor English, I'm still practising it!
I am currently trying to learn how servers and clients work by making a trojan using python and sockets import, my client and server work perfectly on my computer but the moment I send the client to my other laptop the server does not connect. This happens even when i am on the same wifi network.
Server:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 1234
server.bind((host, port))
server.listen(5)
run = True
client, addr = server.accept()
print('Got connection from',addr)
while run:
try:
data = input('>>>')
client.send(data.encode('UTF-8'))
msg = client.recv(1024)
print(msg.decode('UTF-8'))
except ConnectionResetError:
print('Client lost server connection')
print('Trying to connect . . .')
client, addr = server.accept()
print('Got connection from',addr)
Client:
import socket
import os
server = socket.socket()
host = '127.0.0.1'
port = 1234
run = True
server.connect((host,port))
while run:
msg = server.recv(1024)
os.popen(msg.decode('UTF-8'))
server.send('Client online . . .'.encode('UTF-8'))
Your client is connecting to IP 127.0.0.1 (the IPv4 loopback address), which will work only when the server is on the same machine as the client.
When the client and server are on different machines, but still on the same LAN network, the client needs to connect to the server's LAN IP instead. Use netstat or similar tool on the server machine to find its LAN IP. Or, simply have your server code print out its local IPs.
When the client is on another network, it needs to connect to the public WAN IP of the server's LAN router, and that router needs to have port forwarding configured on it to route incoming connections from its WAN IP/Port to the server's LAN IP/port. To get the WAN IP, you will have to look at your router's config, or simply query an external site, like https://api.ipify.org, https://api.my-ip.io/ip, etc from a machine on the LAN, like your server.
Update your client to take in the target host/IP from user input, then it will be able to handle all of these scenarios without having to use different code each time.
I am practicing socket programming using python. I am fimiliar with how to make a simple tcp server and client in local address but I want to know how to make it possible so that I can connect to my own computer from a client app that I built. What modifications do I have to make in this server script? or client?
server:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#The host is set to be the local machine.
address = ("127.0.0.1",1234)
s.bind(address)
s.listen(1)
c , addr = s.accept()
while True:
#do some stuff
c.close()
client:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
address = ("127.0.0.1",1234)
s.connect(address)
while True:
#Do client stuff
s.close()
I'm not allowed to make comments yet. But if you have a client app on another device, you can make the HOST your machines IP that stores the server. IF you're using windows ENTER: ipconfig In your command line argument. I think that or Linux its if config. You should be able to set your address to your machines ip address in order to get the client to connect to your local machine. As you noted, localhost will not work.
I'm trying to write two short python scripts that will connect two or more machines to each other, one as the server and the others as clients. It worked perfectly when testing the client and the server script on the same computer, but when I tried it from another computer the client kept timing out; it couldn't connect to the server. Here's my server code:
import socket
server = socket.socket()
host = "computername"
port = 12345
server.bind((host, port))
server.listen(5)
client, addr = server.accept()
Client code:
import socket
server = socket.socket()
host = "computername"
port = 12345
server.connect((host, port))
Any clue as to why the machines can't connect?
I think, you are changing host variable properly when running both client and server scripts in different machines. Try by changing that properly/or using IP address of server machine.
The communication could be prohibited by a firewall.
To rule out DNS related problem, try IPs instead of hostnames:
# server: listen on all interfaces
server.bind(('', port))
and:
# client: specify server's IP address
server.connect(("192.168.XX.YY", port))
with a real IP address, of course.
I emulated a network topology using mininet. The topology contains two hosts connected by several switches. On host 1 we run a client application which creates a socket and tries to connect to the server application on host 2, it fails however. If I run the client- and server-script locally on one of the two hosts it connects with no problems.
server.py:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 10021))
s.listen(5)
while 1:
(clientsocket, address) = s.accept()
#DO STH.
clientsocket.close()
client.py:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((args['ip'], 10021))
while 1:
#DO STH.
s.close()
Here the code used to execute the commands to start the server and client application
topology.py:
server = net.getNodeByName('host2')
client = net.getNodeByName('host1')
server.cmd('./server.py & > serveroutput')
client.cmd('./client.py -i %serverIP > clientfile' % server.getIP())
Are you using OVS openflow switches in your topology?
If they are openflow enabled, you need to have a SDN controller like Ryu or POX running too. The controller would create a path between the two hosts.
Right host 1 is trying to connect to host 2. Sends some TCP messages to the switch, but the switch doesn't know what to do with so it needs to ask a SDN controller for help. But there is no controller. So the connection failes.
If it was not openflow enabled switches it would have found its way to host 2.
So check if the switch is using openflow.
If you don't use a controller, you should configure the OVS's flow table and allow you data flow.Can you check the connectiong between two host using ping and iperf ?