6

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!

4

3 回答 3

6

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"])
于 2012-10-20T01:32:31.370 回答
3

Probably this is limitation of c9 environment management, so users dont abuse their service too much. I would assume that they have some level of management for Node.js used resources and thus allow these to open ports.

If this was the case and I had to work with cloud9, I would probably approach this as follows: - create Node.js service which would act as a proxy, listening on twisted behalf - create new reactor with overriden listenTCP and listenUDP methods, which would bind these to Node.js proxy service.

The way how proxy would work is as follows, Node.js would initially listen on one "management" TCP port. Then, when twisted service starts up, it would create a TCP connection between Node.js and itself through that port. Then, whenever listenTCP or listenUDP is called, these commands would then be dispatched to Node.js service, which in return would open the port and all of the comunication through that port would then be proxied to twisted through that existing TCP connection.

于 2012-10-22T09:43:15.613 回答
2

It's worth mentioning that Jean-Paul's answer worked for me as well, but I had to use 'address' instead of 'interface':

http_server.listen(int(os.environ.get("PORT")), address=os.environ["IP"])
于 2012-11-03T22:55:51.707 回答