当我运行python -m SimpleHTTPServer 8000或python -m CGIHTTPServer 8000在我的shell中时,我将当前目录的内容托管到互联网上.
我想在浏览时使用命令行中的上述命令使以下cgi_script.py正常工作 192.xxx.x.xx:8000/cgi_script.py
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
Run Code Online (Sandbox Code Playgroud)
但这个脚本按字面显示,而不仅仅是"Hello World!" 部分.顺便说一句,我将文件权限更改为755 cgi_script.py以及我托管它的文件夹.
我一直在寻找将特殊URL重定向到远程服务器以进行一些XSS测试的语法.有任何想法吗?
import SimpleHTTPServer
import SocketServer
class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
print self.path
if self.path == '/analog':
-------------------->return "http://12b.dk/analog"???
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
theport = 1234
Handler = myHandler
pywebserver = SocketServer.TCPServer(("", theport), Handler)
print "Python based web server. Serving at port", theport
pywebserver.serve_forever()
Run Code Online (Sandbox Code Playgroud) 我希望使用本地Web服务器为用户运行一系列python脚本.出于各种不可避免的原因,python脚本必须在本地运行,而不是在服务器上运行.因此,我将使用HTML +浏览器作为用户界面,我很满意,用于前端.
因此,我一直在寻找能够执行python脚本的轻量级Web服务器,它位于机器的后台,理想情况下是Windows服务.安全性和可扩展性不是高优先级,因为它们都在内部在小型网络上运行.
我应该将本机python Web服务器作为Windows服务运行(在这种情况下,如何)?或者将Apache安装到用户的计算机上并以CGI身份运行同样容易?由于这都是本地的,性能也不是问题.
还是我错过了一些明显的东西?
BaseHTTPServer和SimpleHTTPServer有什么区别?我应该在何时何地使用这些?
我正在编写一个包含某个Web服务API的Python模块.这都是REST,所以实现相对简单.
但是,我发现单元测试时遇到了问题:由于我没有运行我为此模块制作的服务,我不想敲打它们,但与此同时,我需要检索数据来运行我的试验.我看了SimpleHTTPServer,没关系.
我解决了我遇到的部分问题,但是现在,因为我似乎无法终止该线程,所以在多次启动测试应用程序时,我遇到了"已经在使用地址"的问题.
这是一些示例代码
PORT = 8001
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), handler)
httpd_thread = threading.Thread(target=httpd.serve_forever)
httpd_thread.setDaemon(True)
httpd_thread.start()
api_data = urllib.urlopen("http://localhost:8001/post/index.json")
print "Data start:"
print json.load(api_data)
Run Code Online (Sandbox Code Playgroud)
其中"index.json"是我制作的模拟JSON文件,它取代了真实的东西.程序终止后如何优雅地清理东西?
我的应用程序使用以下简单的Threaded文件服务器:
class FileServer(Thread):
"""Simple file server exposing the current directory for the thumbnail
creator
"""
def __init__(self, port):
Thread.__init__(self)
self.port = port
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
self.httpd = SocketServer.TCPServer(("", port), Handler)
def run(self):
self.httpd.serve_forever()
def stop(self):
self.httpd.shutdown()
Run Code Online (Sandbox Code Playgroud)
如何防止SimpleHTTPServer在每次请求时输出"GET'http:// BLA BLA BLA"?谢谢
我喜欢使用Python的SimpleHTTPServer来本地开发各种Web应用程序,这些应用程序需要通过Ajax调用等来加载资源.
当我在URL中使用查询字符串时,服务器总是重定向到附加了斜杠的同一URL.
例如,/folder/?id=1重定向到/folder/?id=1/使用HTTP 301响应.
我只是使用启动服务器python -m SimpleHTTPServer.
知道如何摆脱重定向行为吗?这是Python 2.7.2.
我正在尝试用 python 编写一个简单的服务器。因此,在观看教程后,我尝试导入一些模块。
from http.server import HTTPServer
from http.server import SimpleHTTPServer
Run Code Online (Sandbox Code Playgroud)
正如医生所说,它已被移动,这就是我这样做的原因。
但它给了我这个错误:from http.server import SimpleHTTPServer ImportError: Cannot import name 'SimpleHTTPServer'
如果没有SimpleHTTPServer,我就无法使用SimpleHTTPRequestHandler,因为它是在 中定义的SimpleHTTPServer.SimpleHTTPRequestHandler。
我该如何解决这个问题?
你能推荐一个我可以嵌入桌面应用程序的简约python web服务器吗?
我通过调用服务器将文件夹中的文件共享给其他设备python -m SimpleHTTPServer.
我只是试图流使用此与它的视频/音频(标准MP4和MP3,无论是在20MB)到另一台计算机WORKS(但在终端抛出的错误(下上市)).
不知何故,视频/音频失败(除了非常小的mp3文件)在iPhone/iPad上与Safari一起玩.它绝对与媒体文件无关,因为我在iPhone中使用Apache成功传输它们.
知道为什么会这样吗?
Exception happened during processing of request from ('192.168.1.2', 51775)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud) python ×10
simplehttpserver ×10
http ×2
cgi ×1
hosting ×1
httpserver ×1
ios ×1
redirect ×1
server ×1
streaming ×1
unit-testing ×1
url ×1
webserver ×1