I am very new to socket programming. I am trying to connect to a power supply over ethernet. My Mac (OS X) is connected to an ethernet switch and the power supply is also connected to the switch. I have some code written in python to send/receive commands/messages to/from the power supply.
The switch interface allows me to assign a static IP to the supply. It is this same IP that I use as the target IP in the following code:
def __init__( self, IP_TARGET ):
IP = IP_TARGET
PORT = 8080
self.supply = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
self.supply.connect( (IP, PORT) )
This code runs without any issues. My next goal is communicate with the device using the following code:
def getDeviceInfo( self ):
self.supply.send('some command ')
self.supply.recv(10)
Using some print statements I have narrowed the issue down to the recv() command. The send() throws no errors. I get the following error
...in getDeviceInfo
self.supply.recv(10) socket.error: [Errno 61] Connection refused
I am able to ping the device using the IP that I assigned it using the ethernet switch. I have firewall off. I have searched the www for clues as to how I might resolve this error, but to no avail. Any thoughts?
It looks like there is nothing running on the host and port you specified (you can check it with nmap, for example).
In the case it does, is it expecting to receive anything and responding to it?
EDIT: your code is not working because in the remote host and the port you specified (8080), there must be some some code running, listening for messages and responding to them. If there is nothing running on that port, it obviously returns you the "Connection refused" error.
In other words, you created the client, but not the server :P
The port I chose was random (I thought anything above 4096 would be
okay).
See the Digi Connect® Family Command Reference on how to Access the Command Line.
The Command-Line Interface for the Digi device uses the telnet port, which is TCP port 23.
(The port number restrictions you were probably thinking of apply only if you do the server.)
Related
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'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 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).
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....
I'm trying to make an online FPS game and so far it works on my local network. What I'm trying to do is make it work globally
I've tried making other Python projects work globally in the past but so far I haven't been able to get it to work. I get my IP from ipchicken or whatever and put it as the HOST for the server, but when I try to start it I get this.
socket.error: [Errno 10049] The requested address is not valid in its context
I've tried many different versions of what could be my IP address found from various different places, but all of them give that output.
I thought, since I had my webspace, I could try doing what it says you can do in the Python manual:
where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl'
So, I put in the domain of my webspace (h4rtland.p3dp.com) and I get this error:
socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Though only on port 80, anything else gives me the same error as before.
If anybody can shed some light on this subject for me it would be greatly appreciated.
First off, port 80 is typically http traffic. Anything under port 5000 is priviledged which means you really don't want to assign your server to this port unless you absolutely know what you are doing... Following is a simple way to set up a server socket to accept listen...
import socket
host = None #will determine your available interfaces and assign this dynamically
port = 5001 #just choose a number > 5000
for socket_information in socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM):
(family, type, prototype, name, socket_address) = socket_information
sock = socket.socket(family, type, prototype)
sock.bind(socket_address)
max_clients = 1
sock.listen(max_clients)
connection, address = sock.accept()
print 'Client has connected:', address
connection.send('Goodbye!')
connection.close()
This is a TCP connection, for an FPS game you likely want to look into using UDP such that dropped packets don't impact performance terribly... Goodluck