【发布时间】:2012-10-10 14:45:50
【问题描述】:
我刚开始使用 Cloud9 IDE (c9),到目前为止它看起来不错,除了一些小问题。
我从文档中看到,要启动一个简单的 node.js http 服务器,您必须传入 process.env.PORT 来代替常规端口,例如“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);
我想知道的是,在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+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()
通过 documentation 和 github 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)
listenTCP TypeError 很奇怪,因为 2 个参数在本地有效,但在 Cloud9 上无效。我不明白为什么使用这些参数不起作用。
我将上述代码托管在this public Cloud9 项目上,供任何人查看。谢谢!
【问题讨论】:
标签: python port twisted cloud9-ide