I am trying to run a simple Python http server that displays "hello world" on port 8080 using a micro instance. I also have 4 instances of Tornado running behind Nginx. Connecting to Nginx/Tornado on port 80 is not a problem.
I have added port 8080 to my firewall settings, and ensured port 8080 is open and listening on the server but no matter what I do, my connection is always refused. I have tried connecting using browsers, telnet and wget and every single connection is refused.
Here is the output of netstat -an | grep "LISTEN "
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8003 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp6 0 0 :::8000 :::* LISTEN tcp6 0 0 :::8001 :::* LISTEN tcp6 0 0 :::8002 :::* LISTEN tcp6 0 0 :::8003 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN
Here is my iptables list
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:http-alt Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
Here is the Python script I am using:
#!/usr/bin/python from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 8080 #This class will handles any incoming request from #the browser class myHandler(BaseHTTPRequestHandler): #Handler for the GET requests def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() # Send the html message self.wfile.write("Hello World!") return try: #Create a web server and define the handler to manage the #incoming request server = HTTPServer(('', PORT_NUMBER), myHandler) print 'Started httpserver on port ' , PORT_NUMBER #Wait forever for incoming htto requests server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down the web server' server.socket.close()
Use the firewall-cmd command to open a port. To make the change permanent, add the --permanent flag to the command: firewall-cmd --zone=public --permanent --add-port=22/tcp . To open a UDP port, replace tcp with udp . To open the port by service name, use firewall-cmd --zone=public --permanent .
Opening Ports with Firewall RulesFrom the Compute Engine console, click “View Network Details” on the instance. Click on “Firewall Rules” in the sidebar. Create a new firewall rule. Give it a name, and choose whether you want to allow or deny traffic.
Does your network have the corresponding firewall rule? Follow the next steps to create it.
Go to the Developers Console and click on the corresponding project.
Click on 'Compute'
Click on 'Networks'
Click on the name of the corresponding network. You can see in which network is your instance clicking on 'VM instances' under the 'Compute Engine' section or with the command:
gcloud compute instances describe <instance> | grep "network:" | awk -F/ '{print $(NF)}'
Under the Firewall rules section, click 'Create new'
Enter a name for the firewall rule and in the field 'Protocols & ports' type: tcp:8080
Save the rule
After that, you should be able to access your HTTP server.
Otherwise you can try to see if your machine receives the SYN TCP packets in that port with the command: sudo tcpdump -i eth0 port 8080
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With