如何使用Python在Cloud9中声明端口 [英] How to declare ports in Cloud9 using Python

查看:126
本文介绍了如何使用Python在Cloud9中声明端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用 Cloud9 IDE (c9),到目前为止,它看起来不错,除了一些小事情.

I'm new to using Cloud9 IDE (c9) and so far it looks great, except a few minor things.

我从文档中看到,要启动一个简单的node.js http服务器,您必须传递process.env.PORT来代替常规端口(例如"8080").

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".

节点Hello World 示例:

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吗?

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?

我上传了一些扭曲的代码,这些代码对我来说本地可用,但是在c9上不起作用,因为它试图访问本地端口(已经在使用中).这是错误

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.

如果可以的话,如何使以下示例在c9上运行?

How would one make the following example work, if even possible, running on c9?

Python + Twisted Hello World 示例

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()

通过文档和github

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.

更新了下面的输出

节点代码

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上无效.我不明白为什么使用这些参数不起作用.

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.

我将上述代码托管在公共Cloud9项目中,供任何人查看.谢谢!

I have the above code hosted on this public Cloud9 project for anyone to take a look. Thanks!

推荐答案

process.env.PORTprocess.env.IP来自Node.js 在Python中听起来像 os.environ["PORT"]os.environ["IP"].也许您可以尝试:

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"])

这篇关于如何使用Python在Cloud9中声明端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆