mrc*_*mpe 6 python port twisted cloud9-ide
我是使用Cloud9 IDE(c9)的新手,到目前为止看起来很棒,除了一些小问题.
我从文档中看到,要启动一个简单的node.js http服务器,你必须传入process.env.PORT来代替常规端口,例如"8080".
节点Hello World 示例:
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);
Run Code Online (Sandbox Code Playgroud)
我想知道的是,在c9上,你只能使用javascript/node.js在端口上启动服务吗?或者其他语言也可以正常工作,也许还有其他一些传递端口的方法?特别是python + Twisted?
我上传了一些在本地为我工作的扭曲代码,但不能在c9上工作,因为它试图访问本地端口(已经在使用中).这是错误
twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 98] Address already in use.
Run Code Online (Sandbox Code Playgroud)
如果可能的话,如何在c9上运行以下示例?
Python + Twisted Hello World 示例
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()
Run Code Online (Sandbox Code Playgroud)
对文档和github 问题的初步搜索没有太大的影响.我希望这是可能的,我错过了正确的参数传递.
编辑:更新下面的输出
节点代码
console.log(process.env.PORT)
console.log(process.env.IP)
Run Code Online (Sandbox Code Playgroud)
终端输出
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
Run Code Online (Sandbox Code Playgroud)
Python代码
import os
print os.environ["PORT"]
print os.environ["IP"]
Run Code Online (Sandbox Code Playgroud)
终端输出
Running Python Process
8080
127.6.70.129
Run Code Online (Sandbox Code Playgroud)
扭曲的代码
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()
Run Code Online (Sandbox Code Playgroud)
终端输出
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)
Run Code Online (Sandbox Code Playgroud)
该listenTCP类型错误很奇怪,因为2个参数本地工作,但不是在CLOUD9.我不明白为什么使用这些参数不起作用.
我在这个公共Cloud9项目上托管了上述代码,供任何人查看.谢谢!
process.env.PORT
并process.env.IP
从Node.js的听起来像 os.environ["PORT"]
和os.environ["IP"]
Python编写的.也许你可以试试:
reactor.listenTCP(int(os.environ["PORT"]), site, interface=os.environ["IP"])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5950 次 |
最近记录: |