Search code examples
pythonporttwistedcloud9-ide

How to declare ports in Cloud9 using Python


I'm new to using Cloud9 IDE (c9) and so far it looks great, except a few minor things.

I see from the docs that to start up a simple node.js http server, you have to pass in process.env.PORT in place of the regular port such as "8080".

Node Hello World example:

 var http = require('http');
 http.createServer(function (req, res) {
     res.writeHead(200, {'Content-Type': 'text/plain'});
     res.end('Hello World\n');
 }).listen(process.env.PORT, process.env.IP);

What I want to know is, on c9, can you only start services on ports using javascript / node.js? Or do other languages work just as well, perhaps with some other method of passing the port? Specifically python + Twisted?

I uploaded some twisted code that was working locally for me, but wouldn't work on c9 because it was trying to access local ports (which are already in use). Here is the error

twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 98] Address already in use.

How would one make the following example work, if even possible, running on c9?

Python+Twisted Hello World example

from twisted.web import server, resource
from twisted.internet import reactor

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>Hello, world!</html>"

site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()

Initial searches through the documentation and github issues did not turn much up. I'm hoping this is possible and I just missed the right parameter to pass.


Edit: Updated output below

Node Code

console.log(process.env.PORT)
console.log(process.env.IP)

Terminal output

Running Node Process
Tip: you can access long running processes, like a server, at 'http://private-cloud.mrchampe.c9.io'.
Important: in your scripts, use 'process.env.PORT' as port and 'process.env.IP' as host.
8080
127.6.70.129

Python Code

import os

print os.environ["PORT"]
print os.environ["IP"]

Terminal output

Running Python Process
8080
127.6.70.129

Twisted code

import os
import twisted

from twisted.web import server, resource
from twisted.internet import reactor

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>Hello, world!</html>"

site = server.Site(Simple())

reactor.listenTCP(int(os.environ["PORT"]), interface=os.environ["IP"])
reactor.run()

Terminal Output

Running Python Process
hello world
Traceback (most recent call last):
  File "python/hello.py", line 17, in <module>

reactor.listenTCP(int(os.environ["PORT"]), interface=os.environ["IP"])
TypeError: listenTCP() takes at least 3 non-keyword arguments (2 given)

The listenTCP TypeError is strange because 2 arguments works locally but not on Cloud9. I don't see why using these arguments does not work.

I have the above code hosted on this public Cloud9 project for anyone to take a look. Thanks!


Solution

  • process.env.PORT and process.env.IP from Node.js sound like os.environ["PORT"] and os.environ["IP"] in Python. Perhaps you can try:

    reactor.listenTCP(int(os.environ["PORT"]), site, interface=os.environ["IP"])