I would try connect to a counterstrike server hosted on my ip through python socketing ...
import socket
s = socket.socket()
s.connect(("localhost", 27015))
But I would get an error
error: [Errno 10061] No connection could be made because the target machine actively refused it
And I'm sure the server is up, so I'm not sure why it wouldn't connect, I could connect to it in game.
To debug such things you can use Wireshark to observe your Python script while it is trying to connect and compare it to a real client. You may have to listen on the "localhost" interface instead of your physical network interface to see the connection. Your server needs to do the same, so ask your OS to give you a list of bound sockets, including the IPs they are bound to (on Linux netstat -nlp) to check. Maybe the server needs to be configured to allow non-public IPs. Finally, make sure you got the protocol right, TCP (SOCK_STREAM) vs UDP (SOCK_DGRAM).
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'll try be concise, but please let me know if I can provide any more helpful pieces of information.
I have client and server Python programs, and they work fine when ran on the same machine, and when the client connects to my machine's local IP (not 127.0.0.1, but the IP assigned to my machine). I have not been able to get this to work with my public IP.
I get a [Errno 61] Connection refused error when I try to get the client to connect to my router's public IP address. My server binds to all interfaces using bind(("0.0.0.0", 50000)), and I already set up port forwarding for my router. I verified that the program is listening on that port by running netstat -an | grep LISTEN and finding the following line:
tcp4 0 0 *.50000 *.* LISTEN
I can also seemingly reach the port through an online port checking tool, which shows that the port is open when I am running my program, and closed when I close that program. My program also registers the connection from this tool.
The fact that my program accepts the connection from the port checking tool gives me the impression that my client code is missing something, but I can't find any answers. It might be worth noting that I am still running my server and client code on the same machine, but I'm not sure why that would derail things. Here's the code I use to connect on the client side:
tcp_client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcp_client.connect(('my_public_ip', 50000))
Are there any diagnostic steps that I can follow to narrow down my issue?
Before you spend any more time on this, try connecting to your public ip from a computer outside your home network. Spend a couple of dollars on an AWS instance for an hour if you have to, or try connecting from a friend's machine, whatever. It will probably work just fine.
I suspect the problem is simply that you cannot, from inside your home network, connect to your router's public ip address. I tried the same thing with my local network and ran into the same behavior.
If you really need to your public ip during development, you can just assign that as an alias to one of your local interfaces (ip addr add 1.2.3.4/32 dev eth0)...but it's probably easier just to use your an address on your local network, or just arrange for regular access to a remote system for testing.
I am trying to implement a simple TCP instant messaging program in Python using the socket module. When I use the value returned by socket.gethostname() in connect(), the client works perfectly fine when connecting to the IM server running on localhost. However, the client returns errno 111 (Connection refused) when I use the value returned by socket.gethostbyname("localhost"). Is there any way to fix this issue?
If you get a 111 when trying to connect, that generally means nothing is listening at that host and port.
It's usually easier to check this with a one-liner using netcat (which you probably have built-in on any platform but Windows, and can get easily if you don't), or telnet if you must:
abarnert$ nc -v localhost 111
nc: connect to localhost port 111 (tcp) failed: Connection refused
nc: connect to localhost port 111 (tcp) failed: Connection refused
nc: connect to localhost port 111 (tcp) failed: Connection refused
Since netcat couldn't connect, the problem isn't your Python client, it really is that there's nothing to connect to.
This means your server isn't listening on localhost:111.
Without knowing anything about your server beyond a brief mention, it's impossible to diagnose, but my first guess would be that you're doing a bind((gethostname(), 111)), which means it ends up listening only on, say, 10.0.0.3:111.
If you want to listen on all hosts and interfaces, there are fancy ways to specify that, but the easiest way is:
serversocket.bind(('', 111))
I am running a Graphite server to monitor instruments at remote locations. I have a "perpetual" ssh tunnel to the machines from my server (loving autossh) to map their local ports to my server's local port. This works well, data comes through with no hasstles. However we use a flaky satellite connection to the sites, which goes down rather regularly. I am running a "data crawler" on the instrument that is running python and using socket to send packets to the Graphite server. The problem is, if the link goes down temporarily (or the server gets rebooted, for testing mostly), I cannot re-establish the connection to the server. I trap the error, and then run socket.close(), and then re-open, but I just can't re-establish the connection. If I quit the python program and restart it, the connection comes up just fine. Any ideas how I can "refresh" my socket connection?
It's hard to answer this correctly without a code sample. However, it sounds like you might be trying to reuse a closed socket, which is not possible.
If the socket has been closed (or has experienced an error), you must re-create a new connection using a new socket object. For this to work, the remote server must be able to handle multiple client connections in its accept() loop.
For a class project I'm trying to do some socket programming Python but running into a very basic issue. I can't create a TCP connection from my laptop to a lab machine. (Which I'm hoping to use as the "server") Without even getting into the scripts I have written, I've been simply trying interpreter line commands with no success. On the lab machine (kh4250-39.cselabs.umn.edu) I type the following into Python:
from socket import *
sock = socket()
sock.bind(('', 8353))
sock.listen(5)
sock.accept()
And then on my laptop I type:
from socket import *
sock = socket()
sock.connect(('kh4250-39.cselabs.umn.edu', 8353))
At which point both machines block and don't do anything until the client times out or I send a SIGINT. This code is pretty much exactly copied from examples I've found online and from Mark Lutz's book Programming Python (using '' for the server host name apparently uses the OS default and is fairly common). If I run both ends in my computer and use 'localhost' for the hostname it works fine, so I suspect it's some problem with the hostnames I'm using on one or both ends. I'm really not sure what could be going wrong on such a simple example. Does anyone have an idea?
A good way to confirm whether it's a firewall issue or not is to perform a telnet from the command-line to the destination host in question:
% telnet kh4250-39.cselabs.umn.edu 8353
Trying 128.101.38.44...
And then sometime later:
telnet: connect to address 128.101.38.44: Connection timed out
If it just hangs there at Trying and then eventually times out, chances are the connection to the remote host on that specific port is being blocked by a firewall. It could either be at the network layer (e.g. a real firewall or a router access-list) or at the host, such as iptables or other host-based filtering mechanisms.
Access to this lab host might only be available from within the lab or the campus network. Talk with your professor or a network administrator or someone "in the know" on the network to find out for sure.
Try to bind the server to 'kh4250-39.cselabs.umn.edu' instead of '':
sock.bind(('kh4250-39.cselabs.umn.edu', 8353))
If this does not work: Another reason could be a firewall blocking the port 8353....