Is there a way to connect to another computer via their public IP using python sockets, so that you can send data?
In a similar way to which you can connect to a webpage, etc can you do something like this:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 6000))
I have seen examples of people using this in DOS scripts but can't seem to recreate it. All I get is timeout errors. The only time it worked was when using port 5000 and connecting to my own IP (but all other ports failed), any idea why this happened as well?
Related
I am trying to create a peer-to-peer python app using the socket library. I am curious to know if there is any way in which I can use the socket library to connect to another computer outside my local network without any manual steps like opening ports on the router for port forwarding. Do I need to use an already open port on the router (given that routers have some ports open on default)? Please guide me. I am new to socket and networking.
My code till now:-
client-1 (sender)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((MYPUBLICIP, 433))
s.send(b"HELLO!")
s.close()
client 2 (receiver)
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((MYPRIVATEIP, 433))
s.listen()
conn, addr = s.accept()
with conn:
print(f"[CONNECTION_ALERT] Received connection request from {addr}.")
while True:
data = conn.recv(1026).decode('utf-8')
if not data:
break
print(data)
The error I am getting:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
If needed, my python version is 10
Solution to connect two hosts on the same local network
Error message suggest that your firewall probably blocked the network traffic, you can try to disable your firewall and try again, but it is not advisable. If you want to play with network stuff i would suggest you create a local lab that is not connected to internet, like VM's or old laptops/pc.
Solution to connect two hosts NOT on the same local network
If you want to connect two hosts that are not on the same local network then the problem becomes more complicated, and have several possible solutions:
Ask your ISP for public IP (easy but usually comes with extra costs)
Use available software solutions to create private networks over the internet, like hamachi or ngrok (also easy but depending on the usecase cost will vary)
Exploiting naive NAT's by using STUN (hard to implement but satisfying if done right, nice explanation of how it was implemented in OpenTTD multiplayer)
I’m trying to create a server on my raspberry pi using python and then i want to test the server by accessing it from another device using the IP address of the raspberry pi, but the problem is that everytime i type the IP address of my raspberry pi into my webpage, it doesn’t open and i don’t know if there’s a problem in my code or not, i will write below so that anyone could check
import socket
import sys
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
try:
my_socket.bind((host, 1234))
except socket.error:
print(‘failed’)
sys.exit()
my_socket.listen(5)
While True:
conn, addr = my_socket.accept()
data = conn.recv(1000)
if data:
print(‘got a request’)
my_socket.send(‘Thank you’)
my_socket.close()
conn.close()
after that i tried typing the raspberry pi’s IP address on my laptop’s webpage but it was no use, first i typed ifconfig in the terminal of the raspberry pi to get the IP address and i tried it but it didn’t work, then i added another line in the code which is gethostbyname to the variable host and printed it and it showed different IP address than the one in ifconfig which was confusing to me, but i even tried this another IP address on my webpage and it didn’t work too
Did you check with netstat to see if the code is actually listening? Should list the <IP>:<Port> as LISTENING, i always check that when i do server code.
Sometimes when you try out code and don't terminate it properly, there can be a orphan process still listening to the Interface:Port hogging the port. Been there, done that, got the T-shirt.
Also, try using 0.0.0.0 instead, it tells the socket listener to listen on all interfaces, including loopback.
Got any firewall denying the connection ? Check that.
Also, check try using curl as a debug tool and see if 1) Curl can connect and 2) you get send some HTTP data to the server:
Curl 127.0.0.1:1234/HelloWorld
I am trying to create an android app which connects to a socket created on the desktop. I did a lot of testing and figured out that my routing was giving me a hard time.
When I tried to connect over a mobile hotspot, it was working perfectly fine. But when I connected over the router, my connection was getting rejected or something. I tried connecting both ways, one with my android as serversocket and one with my desktop as server socket.
I think the issue is because of port forwarding or something, but since I'm new to networking, I don't know how to configure it.
I looked up online and tried some stuff myself, however nothing worked.
This is how my D-Link router port forwarding setup is.
D-Link router
I have a socket running at port 8080 hosted by my android device with ip 192.168.0.122 and my laptop is at ip 192.168.0.123
This is client code which i have on my laptop(client.py)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = input("IP: ")
port = int(input("Port: "))
s.connect((ip,port))
print(s)
s.send(bytes(str("Hello!"), "utf-8"))
I am using python(spyder) to connect as client by enterring the socket ip address and the port number.
The main problem I am facing is the weird behaviour of it working sometimes and then after like a few hours, it just stops working without doing anything. Then I try the same method I did last time to make it work, but it still doesn't work. I feel like my router is moody and only works when it wants to, and that is the weird behaviour.
I'm having some trouble understanding the security of sockets. I have some code set up in Python, which looks a bit like this:
server.py:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)
client.py:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1234)
I set myself a project to make a VOIP over LAN, or as close as I can get. I'll be using strictly private IP addresses in this program. I read that opening ports can be dangerous, so I'm a little nervous.
As you can see, I am using the port 1234. Could this introduce a vulnerability from any external threats?
When people say "opening ports can be dangerous" what they mean is that making an application available to connections from an unknown source (such as the Internet) is risky. There may be bugs in the application that allow a remote attacker to execute commands, for example.
As long as you don't accept connections directly from the Internet you're probably fine. If an attacker has access to your LAN, you have more serious things to worry about.
I'm building an RPC Server in golang that uses msgpack.
The client is built in python using the mprpc library (msgpack over TCP with gevent).
My issue is, being an absolute noob in networking, I discovered that I can't use the same address/port with multiple clients running at once on the same computer (socket already bound i guess, it just stalls and timeouts).
I have looked around quite a bit but I'm not sure what I should be doing to be able to have multiple clients on the same machine talk to a server (msgpack back and forth). Is this a case where I need to use ZeroMQ ? Or requests over HTTP ?
Thanks !
TCP is a connection-oriented protocol. This means that only the server needs to have a fixed, known port. The client can use any port it wants, because nobody is making a connection to the client.
So, how does the server know how to talk to the client? Whenever it accepts a connection, it's told who the connection is from. But usually, you don't even need that, because the socket keeps track of who the connection is from. Just recv and send on that socket, and you're talking to the right client.
You should probably read the Socket Programming HOWTO in the Python docs, or some other tutorial, but briefly…
A server starts like this:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 12345))
sock.listen(5)
while True:
csock, addr = sock.accept()
It binds a port and listens and loops around accepting connections and doing something with them.
A client, on the other hand, just does this:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 12345))
… or, equivalently:
sock = socket.create_connection(('localhost', 12345))
It doesn't call bind, it just creates a connection, letting the sockets library pick an arbitrary port on the appropriate interface for that connection. Unless you've got thousands of sockets already open, it should always be able to find a free port for you.
If you want to have two way connection, then HTTP is not suitable for this. Because HTTP is designed in a way that the server only responds to a request, which prevents server to issue a request itself. There are other solutions that provide two way connection(server to client and client to server in same time).
WebSocket is the first thing that comes to my mind. Of course ZeroMQ also can do this.