博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tech Tip: Really Simple HTTP Server with Python
阅读量:6155 次
发布时间:2019-06-21

本文共 1901 字,大约阅读时间需要 6 分钟。

Tech Tip: Really Simple HTTP Server with Python

 in

If you need a quick web server running and you don't want to mess

with setting up apache or something similar, then can help.
Python comes with a simple builtin HTTP server.
With the help of this little HTTP server you can turn any directory in
your system into your web server directory.
The only thing you need to have installed is Python.

Practically speaking this is very useful to share files inside

your local network. Implementing this tiny but hugely useful HTTP
server is very simple, its just a single line command.

Assume that I would like to share the directory /home/hisam and my IP

address is 192.168.1.2

Open up a terminal and type:

$ cd /home/somedir$ python -m SimpleHTTPServer

That's it!

Now your http server will start in port 8000. You will get the message:

Serving HTTP on 0.0.0.0 port 8000 ...

Now open a browser and type the following address:

http://192.168.1.2:8000

You can also access it via:

http://127.0.0.1:8000

If the directory has a file named index.html, that file will be

served as the initial file. If there is no index.html, then
the files in the directory will be listed.

If you wish to change the port that's used start the program via:

$ python -m SimpleHTTPServer 8080

If you want to only serve on localhost you'll need to write

a custom Python program such as:

import sysimport BaseHTTPServerfrom SimpleHTTPServer import SimpleHTTPRequestHandlerHandlerClass = SimpleHTTPRequestHandlerServerClass  = BaseHTTPServer.HTTPServerProtocol     = "HTTP/1.0"if sys.argv[1:]:    port = int(sys.argv[1])else:    port = 8000server_address = ('127.0.0.1', port)HandlerClass.protocol_version = Protocolhttpd = ServerClass(server_address, HandlerClass)sa = httpd.socket.getsockname()print "Serving HTTP on", sa[0], "port", sa[1], "..."httpd.serve_forever()

Note also that this should also work on Windows or .

转载地址:http://sfbfa.baihongyu.com/

你可能感兴趣的文章
Oracle技术之如何监测一个PLSQL过程的运行情况(三)
查看>>
健身:手臂训练
查看>>
Unable to preventDefault inside passive event listener due to target being treated as passive.
查看>>
【趣文翻译】如何用各种编程语言杀死一条龙,PHP大亮 [转]
查看>>
[Asp.net MVC]Asp.net MVC5系列——第一个项目
查看>>
[Node.js]DNS模块
查看>>
响应式编程(Reactive Programming)(Rx)介绍
查看>>
数据之独立存储(Isolated Storage)[转]
查看>>
[数据结构]快速排序
查看>>
面试题----网页/应用访问慢突然变慢,如何定位问题
查看>>
对Java单例模式 volatile关键字作用的理解
查看>>
sublime Text3支持vue高亮,sublime Text3格式化Vue
查看>>
我的第一篇博客
查看>>
神经网络CNN训练心得--调参经验
查看>>
网站防刷方案
查看>>
【成长之路】【python】python基础5-模块
查看>>
第一次程序改错
查看>>
陶哲轩谈数学家的合作(来自陶哲轩在数学家Gowers的博文“Is massively collaborative mathematics possible?”上的评论)...
查看>>
良序集的势的三歧性
查看>>
陶哲轩实分析 习题 12.5.13
查看>>