首页 \ 问答 \ Python SimpleHTTPServer更改服务目录(Python SimpleHTTPServer Change Service Directory)

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
更新时间:2021-05-26 09:05

最满意答案

在服务器中:

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.innerWidthwindow.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 and window.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>

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)