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

本文共 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://fvbeo.baihongyu.com/

你可能感兴趣的文章
Gnome 3.2 发布计划及新功能
查看>>
已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性...
查看>>
利用bobo-browse 实现lucene的分组统计功能
查看>>
/MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题
查看>>
基于SGIP协议编写短信网关接口
查看>>
NSCharacterSet 去除NSString中的空格
查看>>
ubuntu server 使用parted分区
查看>>
自定义网页日历
查看>>
solr实现满足指定距离范围条件的搜索
查看>>
ubuntu vsftp安装
查看>>
[转载]Web前端研发工程师编程能力飞升之路
查看>>
Redis
查看>>
XINS 3.0 正式版发布,远程 API 调用规范
查看>>
sqlserver 2005 64bit express
查看>>
(转)Oracle中For和while及一些应用
查看>>
jQuery基础及选择器
查看>>
DragonFly BSD 3.2 发布
查看>>
Mozilla 发布 Popcorn Maker,在线创作视频
查看>>
C#中为什么需要装箱拆箱操作?
查看>>
PHP类中一般方法与静态方法的疑问
查看>>