7

I start a server using sockets and want to allow clients to connect to it.

self.sock.bind(('0.0.0.0',0)) # 0.0.0.0 will allow all connections and port 0 -> os chooses a open port.
stroke_port=self.sock.getsockname()[1]
self.sock.listen(75)
self.open_port_popup(stroke_port)

Now, for other clients to connect I have port forward it manually and it works fine. enter image description here

I want to do this in automated fashion. -> I try upnp.

import miniupnpc

def open_port(port_no):
    '''this function opens a port using upnp'''
    upnp = miniupnpc.UPnP()

    upnp.discoverdelay = 10
    upnp.discover()

    upnp.selectigd()

    # addportmapping(external-port, protocol, internal-host, internal-port, description, remote-host)
    result=upnp.addportmapping(port_no, 'TCP', upnp.lanaddr, port_no, 'testing', '')
    return result

It opens a port shown in the image below. But the port-forwarding list shown in the first image is empty. It doesn't work and clients can't connect. How can I fix this? What am I missing? enter image description here

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • Can you connect locally? – Brian Cain Mar 21 '16 at 04:21
  • @BrianCain Yes, using 127.0.0.1 on same machine. – Abhishek Bhatia Mar 21 '16 at 04:22
  • 1
    I think the "port forwarding list in the first image" only shows manual mappings. – ivan_pozdeev Mar 28 '16 at 11:33
  • As for why clients can't connect, now it's up to you to diagnose this: check the gist of the network issue (if the external port is not open, not forwarding packets correctly in either direction, etc.). If your router indeed turns out to be the root cause, dealing with it is off topic at SO. Your program looks okay according to the example at http://stackoverflow.com/questions/4391872/python-open-a-listening-port-behind-a-router-upnp. – ivan_pozdeev Mar 28 '16 at 12:10
  • Upnp not allowed DHCP address, need static IP on router. Listen port is 4000 and 35716 is out port . Your module or UPNP used a external port (Maybe 1900), need ports redirecting for security(protect your other local devices). Don't use devices with default ports(on LAN). – dsgdfg Apr 03 '16 at 20:18

4 Answers4

4

I think you made a mistake using upnp.lanaddr as internal-host address. upnp.lanaddr is the address of the upnp device which is your router, you want to use the local address of your server.

If needed take a look at Finding local IP addresses using Python's stdlib if you want to get your server local IP dynamically.

Community
  • 1
  • 1
MajorTom
  • 327
  • 2
  • 5
1

I think that we are missing lot of related info to know what's the main problem here. I see so many people guessing.

By the way, just editing that line

result=upnp.addportmapping(port_no, 'TCP', upnp.lanaddr, port_no, 'testing', '') to

result=upnp.addportmapping('7777', 'TCP', '192.168.1.8', '7777', 'testing', '') would tell you if it works at all. Doing port testing from localhost it's dummy, you're not under the router at all.

Also, remember to use Try/Except blocks to tell you what's wrong on your code.

try:
    print "1" + 1
except Exception as e:
    print str(e) 

Another way, not fashioned is to use html/web automation, even cURL to make those requests instead using uPnp, this way you don't really need to handle it.

m3nda
  • 1,986
  • 3
  • 32
  • 45
0

Most of the time ISP don't allow port forwarding, and you spend hours on this trying to forward port.

I went for ngrok - it's a lightweight free of cost (for basic usage) program that tunnels the port and give its own tunneled domain which can be accessed everywhere.

halfer
  • 19,824
  • 17
  • 99
  • 186
LakshayGMZ
  • 51
  • 5
-2

this is interesting question. from what I could summon I think

GUI shows that UPNP port forwarding rules are added. so Most likely there is issue in UPNPC configuration. I doubt you are doing this on Router or similar platform with X-WRT or OpenWRT

the issue I think is you can't use upnp for this or it doesn't work for some strange reason.

I suggest you try this library pytables.

I know you wanted to know why and I am working on figuring out the reason.

this is just for you to get going on this project

and for quick solution

Try this

 import subprocess

p = subprocess.Popen(["iptables", "-A", "INPUT", "-p", "tcp", "-m", "tcp", "--dport", "22" , "-j", "ACCEPT"], stdout=subprocess.PIPE)
        output , err = p.communicate()
        print output
Devidas
  • 2,479
  • 9
  • 24
  • Hi and this is ready made code I found it [here](http://stackoverflow.com/questions/20734319/how-to-write-specific-iptables-rules-using-python-iptables) . `import subprocess p = subprocess.Popen(["iptables", "-A", "INPUT", "-p", "tcp", "-m", "tcp", "--dport", "22" , "-j", "ACCEPT"], stdout=subprocess.PIPE) output , err = p.communicate() print output` – Devidas Mar 28 '16 at 12:08
  • 2
    Please [edit] your answer to include code or other things that answer the question, rather than tacking on a comment. – Ajean Mar 28 '16 at 15:24
  • @Devidas Hi, thx for answer! I tried your code, it gives me this error:`>>> p = subprocess.Popen(["iptables", "-A", "INPUT", "-p", "tcp", "-m", "tcp", "--dport", "22" , "-j", "ACCEPT"], stdout=subprocess.PIPE) ` `Traceback (most recent call last): File "", line 1, in File "c:\Anaconda\lib\subprocess.py", line 710, in __init__ errread, errwrite) File "c:\Anaconda\lib\subprocess.py", line 958, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified`. Please let me know in case I am missing something. – Abhishek Bhatia Mar 29 '16 at 17:18
  • I think the code I have submitted assuming you got some kind of linux machine. – Devidas Mar 30 '16 at 18:13
  • one more thing you have not specified platform so it is bit difficult for me to answer. SO please add that information and have you tried pytables but from error I figured it out this is windows machine this error is caused because there is no binary called iptables on windows. – Devidas Mar 30 '16 at 18:43
  • from screen shot you have submitted and some google search I have intution that the images are of some of Netgear Router running Netgear genie firmware please confirm the platform and OS so that it will be easy to give solution. – Devidas Mar 30 '16 at 18:57
  • @Devidas thanks so for the reply again! You are correct it is windows machine running Netgear genie firmware. – Abhishek Bhatia Apr 01 '16 at 10:45
  • @Abhishek I get it you are running Netgear genie. but to my knowledge it can be run on hardware and its app on windows. So can you explain in short where the web server is running on router or windows machine I am asking this for I think there is confusion on portforwarding as per above sceenshots port forward maybe on router and you are expecting them on your windws machine also explain if you are controling netgear genie using python – Devidas Apr 03 '16 at 06:43