我刚开始使用Cloud9 IDE http://c9.io(c9) 到目前为止,除了一些小问题之外,它看起来很棒。
我从文档中看到,要启动一个简单的node.js http服务器,您必须传递process.env.PORT来代替常规端口,例如“8080”。
节点你好世界example https://docs.c9.io/writing_nodejs_hello_world.html:
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);
我想知道的是,在c9上,只能使用javascript/node.js在端口上启动服务吗?或者其他语言是否也可以工作,也许可以使用其他传递端口的方法?特别是 python + Twisted?
我上传了一些在本地工作的扭曲代码,但在 c9 上不起作用,因为它试图访问本地端口(已在使用中)。这是错误
twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 98] Address already in use.
如果可能的话,如何使以下示例在 c9 上运行?
Python+扭曲的Hello Worldexample http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html
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()
初步搜索通过文档 https://docs.c9.io/和githubissues https://github.com/ajaxorg/cloud9/issues没有出现太多。我希望这是可能的,但我只是错过了要传递的正确参数。
编辑:更新了下面的输出
节点代码
console.log(process.env.PORT)
console.log(process.env.IP)
端子输出
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代码
import os
print os.environ["PORT"]
print os.environ["IP"]
端子输出
Running Python Process
8080
127.6.70.129
扭曲的代码
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()
端子输出
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 监听TCP http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.interfaces.IReactorTCP.listenTCP.htmlTypeError 很奇怪,因为 2 个参数在本地有效,但在 Cloud9 上无效。我不明白为什么使用这些参数不起作用。
我将上面的代码托管在this https://c9.io/mrchampe/python-twisted公共 Cloud9 项目供任何人查看。谢谢!