Python SimpleHTTPServer更改服务目录(Python SimpleHTTPServer Change Service Directory)
我编写了以下代码来启动HTTP服务器,以后可以选择启动TCP / IP服务器:
import SimpleHTTPServer import SocketServer import time import socket def choose(): if raw_input("Would you like to start an HTTP or TCP/IP Server?: ") == "HTTP": print "You have selected HTTP server." if raw_input("Is this correct? Use Y/N to answer. ") == "Y": print "" start_HTTP() else: choose() else: print "Goodbye! " def start_HTTP(): PORT = int(raw_input("Which port do you want to send out of? ")) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), Handler) print "Please wait one moment..." time.sleep(2) run_HTTP(PORT, httpd) def run_HTTP(PORT, httpd): print "Use the following IP address to connect to this server (LAN only): " + [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] #prints local IP address print "Now serving from port: ", PORT print "To shutdown the server, use the PiServer_Off software." time.sleep(2) print "" print "Any traffic through the server will be recorded and displayed below: " httpd.serve_forever() choose()
我想更改目录,以便在将来,除了主机之外没有人可以终止服务器(因为PiServer Off软件将安装在同一目录中)。
我找到了这个解决方案,但它看起来是一个shell,我不知道如何为我的代码修改它(使用Pycharm): http : //www.tecmint.com/python-simplehttpserver-to-create-webserver -或-服务-文件-即刻/
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
我也发现了这个但它似乎没有回答我的问题: 改变目录Python SimpleHTTPServer使用
我想知道是否有人可以分享和解释他们更改目录的方式而不移动服务器文件,因为我想用它来构建一个家庭文件共享系统。
谢谢!
I wrote the following piece of code to start an HTTP server, with a future option of being able to start a TCP/IP server instead:
import SimpleHTTPServer import SocketServer import time import socket def choose(): if raw_input("Would you like to start an HTTP or TCP/IP Server?: ") == "HTTP": print "You have selected HTTP server." if raw_input("Is this correct? Use Y/N to answer. ") == "Y": print "" start_HTTP() else: choose() else: print "Goodbye! " def start_HTTP(): PORT = int(raw_input("Which port do you want to send out of? ")) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), Handler) print "Please wait one moment..." time.sleep(2) run_HTTP(PORT, httpd) def run_HTTP(PORT, httpd): print "Use the following IP address to connect to this server (LAN only): " + [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] #prints local IP address print "Now serving from port: ", PORT print "To shutdown the server, use the PiServer_Off software." time.sleep(2) print "" print "Any traffic through the server will be recorded and displayed below: " httpd.serve_forever() choose()
I want to change the directory so that at a future point, no one but the host machine can terminate the server (since the PiServer Off software will be installed in the same directory).
I found this solution, but it looks to be for a shell, and I do not know how to modify it for my code (using Pycharm): http://www.tecmint.com/python-simplehttpserver-to-create-webserver-or-serve-files-instantly/
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
I also found this but it does not seem to answer my question: Change directory Python SimpleHTTPServer uses
I was wondering if anyone could share and explain their way of changing the directory without moving the server file, as I want to use this to build an in-home file-sharing system.
Thanks!
原文:https://stackoverflow.com/questions/31251524
最满意答案
在服务器中:
socket.emit('screen'); socket.on('size', data => { let width = data.width, height = data.height; //some code here });
在客户端(使用jQuery):
socket.on('screen', () => { let width = $(window).width(), height = $(window).height(); socket.emit('size', {width, height}); });
如果你不使用jQuery,你可以分别发送
window.innerWidth
和window.innerHeight
。您可以在客户端轻松完成。 看看这个简单的例子:
server.js
const express = require('express'); const app = express(); app.get('/', (req,res) => { res.sendFile(__dirname + '/hello.html'); }); app.get('/small', (req,res) => { res.sendFile(__dirname + '/small.html'); }); app.get('/game', (req,res) => { res.sendFile(__dirname + '/game.html'); }); app.listen(3000);
hello.html的
<html> <body> <h1>Hello</h1> <script> if (window.innerWidth < 500 || window.innerHeight < 500) { window.location.assign('small'); } else { window.location.assign('game'); } </script> </body> </html>
small.html
<html> <body> <h1>Small screen :-(</h1> </body> </html>
game.html
<html> <body> <h1>Loading The Game</h1> </body> </html>
in the server:
socket.emit('screen'); socket.on('size', data => { let width = data.width, height = data.height; //some code here });
in the client (using jQuery):
socket.on('screen', () => { let width = $(window).width(), height = $(window).height(); socket.emit('size', {width, height}); });
If you don't use jQuery you can send
window.innerWidth
andwindow.innerHeight
respectively.You can do easily it at the client side. Have a look at the simple example:
server.js
const express = require('express'); const app = express(); app.get('/', (req,res) => { res.sendFile(__dirname + '/hello.html'); }); app.get('/small', (req,res) => { res.sendFile(__dirname + '/small.html'); }); app.get('/game', (req,res) => { res.sendFile(__dirname + '/game.html'); }); app.listen(3000);
hello.html
<html> <body> <h1>Hello</h1> <script> if (window.innerWidth < 500 || window.innerHeight < 500) { window.location.assign('small'); } else { window.location.assign('game'); } </script> </body> </html>
small.html
<html> <body> <h1>Small screen :-(</h1> </body> </html>
game.html
<html> <body> <h1>Loading The Game</h1> </body> </html>
相关问答
更多-
Node.js与ZeroMQ + Socket.io +每个客户端的范围(Node.js with ZeroMQ + Socket.io + scoping per client)[2022-11-28]
我不是CS或ZMQ的人,但听起来你正在向太多的客户广播数据。 io.sockets.emit会将事件发送到所有连接的套接字。 如果要将数据仅发送到1个套接字,则应使用socket.emit ,其中socket已设置为单个套接字对象。 I'm not a CS or ZMQ guy either but it sounds like you're broadcasting data to too many clients. io.sockets.emit will emit the event to all ... -
Windows Azure网站使用IISNode在IIS内部托管Node进程。 您的Node站点实际上是一个命名管道,它接收传入的请求,而不是您在本地运行或自己托管时使用的TCP端口。 即使您可以打开TCP端口,Azure网站实际上只适用于传统网站,并且没有办法打开外部世界的端口。 因此,首先,您需要更改代码中的端口,例如: var port = process.env.PORT||3000; //which you can run both on Azure or local var server = a ...
-
对于将来在这里游荡的人。 我从Pomelo开发者那里听到了回复,他们说“这是我们的内部框架名为colorbox ......它是一个node.js样式的浏览器侧模块系统,源自cocos2d-javascript” For anyone wandering through here in the future. I heard back from the Pomelo devs and they said this "This is our inner framework named colorbox ... ...
-
如果添加一个充当简单Web服务器的node.js组件,则可以使用node-webkit打包资产+ Web服务器。 包含节点+ Chromium。 If you add a node.js component that acts as a simple web server then you can package the assets + the web server with node-webkit. Contains node + Chromium.
-
我刚刚创建了你想在客户端和服务器之间尝试json数据连接的skiliton,它可以检查下面的代码 服务器端 : var http = require("http"); var url = require("url"); var path = require("path"); var ServerIP = '127.0.0.1', port = '8080'; var Server = http.createServer(function (request , response) { ...
-
假设您的Python对象足够简单(不是类的实例,比如说),只需将它们的JSON表示( json.dumps() )发送到socket.io端即可。 我假设你可以在客户端解析JSON,如果需要的话。 Assuming your Python objects are simple enough (not instances of classes, say), just send a JSON representation (json.dumps()) of them to the socket.io side. ...
-
在服务器中: socket.emit('screen'); socket.on('size', data => { let width = data.width, height = data.height; //some code here }); 在客户端(使用jQuery): socket.on('screen', () => { let width = $(window).width(), height = $(window) ...
-
从节点HTTP服务器发送的响应没有大小限制。 您的应用程序可以访问的内存量是唯一的限制。 There is no size limit on responses sent from a node HTTP server. The amount of memory your application has access to is the only limitation.
-
正如加里所说,你绝对应该首先阅读文档,尤其是入门指南。 Stackoverflow不是为每个初学者提供自己的指南,这就是为什么有些人会对这些问题进行投票。 但要回答你的问题: http.createServer(app).listen(port, [hostname], [backlog], [callback]) 没有给回调函数的参数。 这就是req和res undefined 。 因此,您可以将代码更改为: app.listen(port, function(){ console.log("Ser ...
-
Node.js发送文件给客户端(Node.js send file to client)[2022-10-03]
你需要设置一些标题标志; res.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stat.size, 'Content-Disposition': 'attachment; filename=your_file_name' }); 用下载替换流媒体; var file = fs.readFile(filePath, 'binary'); res.setHeader('Content-Length' ...