我是使用Cloud9 IDE(c9)的新手,到目前爲止它看起來不錯,除了一些小的事情。如何使用Python在Cloud9中聲明端口
我從文檔中看到,爲了啓動一個簡單的node.js http服務器,您必須在process.env.PORT中代替常規端口(如「8080」)。
節點的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 +扭曲?
我上傳了一些在本地工作的扭曲代碼,但在c9上無法工作,因爲它試圖訪問本地端口(已經在使用)。以下是錯誤
twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 98] Address already in use.
一個人怎麼會讓下面的例子中工作,如果連可能的話,在C9運行?
的Python +扭曲的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初始檢索並沒有把太多了。我希望這是可能的,我只是錯過了正確的參數傳遞。
編輯:下面
更新的輸出節點代碼
端子輸出
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類型錯誤很奇怪,因爲2個參數本地工作,但不是在CLOUD9。我不明白爲什麼使用這些參數不起作用。
我有上面的代碼託管在this公有Cloud9項目,供任何人看一看。謝謝!
謝謝! os.environ命令*顯示與node.js報告相同的值。但是,似乎python/Twisted仍然沒有綁定到端口。我認爲這要麼是因爲給reactor.listenTCP(給出錯誤)的參數,要麼是因爲平臺不允許。 – mrchampe
對不起,我的答案出錯。 –
這個工程,非常感謝你! – mrchampe