Search code examples
pythonlinuxtcpfirewalliptables

Simple python server "connection refused" error


I found code for a very simple python server and client. When i first ran it I ran the server and client on the same machine in different terminals and it worked fine. However now I am running the server on my raspberry pi and running my client on my desktop and I am getting a "connection refused error. My instincts tell me that It is the firewall blocking the port I am using but I used the following command I found to open the port.

sudo iptables -A INPUT -i eth0 -p tcp --sport 4042 -m state --state ESTABLISHED -j ACCEPT

Both my client and server are connecting to this port # in my python code. When I run sudo nmap -sS -O 127.0.0.1 on my pi I get the following output:

Starting Nmap 6.00 ( http://nmap.org ) at 2013-08-29 23:52 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00027s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
3389/tcp open  ms-wbt-server
No exact OS matches for host (If you know what OS is running on it, see http://nmap.org/submit/ ).
TCP/IP fingerprint:
OS:SCAN(V=6.00%E=4%D=8/29%OT=22%CT=1%CU=37241%PV=N%DS=0%DC=L%G=Y%TM=521FDEE
OS:0%P=armv7l-unknown-linux-gnueabi)SEQ(SP=103%GCD=1%ISR=105%TI=Z%CI=Z%II=I
OS:%TS=7)OPS(O1=M400CST11NW3%O2=M400CST11NW3%O3=M400CNNT11NW3%O4=M400CST11N
OS:W3%O5=M400CST11NW3%O6=M400CST11)WIN(W1=8000%W2=8000%W3=8000%W4=8000%W5=8
OS:000%W6=8000)ECN(R=Y%DF=Y%T=41%W=8018%O=M400CNNSNW3%CC=Y%Q=)T1(R=Y%DF=Y%T
OS:=41%S=O%A=S+%F=AS%RD=0%Q=)T2(R=N)T3(R=N)T4(R=Y%DF=Y%T=41%W=0%S=A%A=Z%F=R
OS:%O=%RD=0%Q=)T5(R=Y%DF=Y%T=41%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)T6(R=Y%DF=Y%T=
OS:41%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T7(R=Y%DF=Y%T=41%W=0%S=Z%A=S+%F=AR%O=%RD=0
OS:%Q=)U1(R=Y%DF=N%T=41%IPL=164%UN=0%RIPL=G%RID=G%RIPCK=G%RUCK=G%RUD=G)IE(R
OS:=Y%DFI=N%T=41%CD=S)

Network Distance: 0 hops

OS detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 29.35 seconds

and when I run iptables -L i get:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp spt:4042 state ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  

Solution

  • I wanted to revisit this to say that while it was not a problem with the ip tables on my raspberry pi, my connection was being blocked by a firewall on the computer I was using as the client of the server.

    To anyone else attempting this experiment, I advise you to add exceptions for the client and host in each others firewalls to avoid this problem.