一个支持上传的简单 HTTP Server

一个支持上传的简单 HTTP Server

{ 撰文/bones7456 }

现在,很多人都知道,Python 里有个 SimpleHTTPServer,可以拿来方便地共享文件。比如,你要发送某个文件给局域网里的同学,你只要 cd 到所在路径,然后执行这么一行:

python -m SimpleHTTPServer

人家就可以通过 http://你的IP:8000 来访问你要共享的文件了。像我早已把这个命令做了 alias。但是,某一天,你需要从同学哪里复制一个文件到本机,然后你就会跟你同学说,XX,共享下某目录。当你以为可以用 HTTP 来访问他的 8000 端口的时候,他却告诉你,不好意思,我是 Windows 啦~~当然你可以选择在他 Windows 里装个 Python,也可以选择使用 Samba、FTP 等其他方式,但是有没有和之前一样简单的方式呢~当然了,这时候,你就需要一个支持上传的简单 HTTP Server,也就是我这个:SimpleHTTPServerWithUpload.py,哈哈。然后你开个服务,让人家上传即可。

其实这个就是修改自 SimpleHTTPServer 的,只不过我给它加上了最原始的上传功能,安全性方面没有验证过,不过理论上应该不会没人一直开着这个吧?另外,我对 RFC1867 的理解不一定透彻,所以,Use on your own risk!

截图如下:

simple-http-server

代码在此,单文件、零配置,直接用 Python 运行。

Source. Thanks bones7456. }

Read More:


38 Comments

  1. 1soquick 评论 @ 2010-05-16 16:15Reply to this comment

    这是骨头吗?坐上这个沙发,俺很荣幸,而且这功能很实用!

  2. 2wudaown 评论 @ 2010-05-16 16:41Reply to this comment

    提问 .. 咱浏览到一些乱码文件 .. 求解

  3. 3lishuai 评论 @ 2010-05-16 16:50Reply to this comment

    好东西啊!为啥咱也有乱码?中文文件名

  4. 4黑日白月 评论 @ 2010-05-16 16:51Reply to this comment

    这个不错~支持骨头兄~

  5. 5wudaown 评论 @ 2010-05-16 16:52Reply to this comment

    @lishuai: + 1 .. 同样中文就有了 .. 浏览器编码应该没问题的说

  6. 6levon 评论 @ 2010-05-16 16:53Reply to this comment

    就知道是骨头写的 因为昨天在他博客那里看到了

  7. 7elfish 评论 @ 2010-05-16 17:10Reply to this comment

    @wudaown:

    记事本打开 寻找f.write("\nDirectory listing for %s\n" % displaypath) 修改为 f.write("\n\n\nDirectory listing for %s\n\n" % displaypath) 即可...

    相对应html源代码在head标签内

  8. 8bones7456 评论 @ 2010-05-16 17:11Reply to this comment

    @wudaowm @lishuai 中文乱码的话,SimpleHTTPServer本身也是一样的,不过如果server的locale是utf8,浏览器的默认编码的也是utf8的话,应该是没问题的。

  9. 9elfish 评论 @ 2010-05-16 17:14Reply to this comment

    <sss>测试一下留言html

  10. 10elfish 评论 @ 2010-05-16 17:16Reply to this comment

    记事本打开 寻找f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath) 修改为 f.write("<html>\n<head>\n<meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\">\n<title>Directory listing for %s</title>\n</head>\n" % displaypath) 即可...

    相对应html源代码在head标签内 <meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\">

  11. 11elfish 评论 @ 2010-05-16 17:19Reply to this comment

    占个地方研究一下这里留言的字符过滤

    记事本打开

     

    寻找f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)

     

    修改为

     

    f.write("<html>\n<head>\n<meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\">\n<title>Directory listing for %s</title>\n</head>\n" % displaypath)

     

    即可...

     

    相对应html源代码在head标签内

     

    <meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\">

     

  12. 12elfish 评论 @ 2010-05-16 17:22Reply to this comment

    另浏览器字符编码设为自动检测也OK了:P

  13. 13wudaown 评论 @ 2010-05-16 17:27Reply to this comment

    @bones7456: 哦哦 .. 还是编码的问题 .. 咱去看看

  14. 14wudaown 评论 @ 2010-05-16 17:30Reply to this comment

    @wudaown: 感谢 .. 原来firefox不是默认utf-8的..

  15. 15三爷 评论 @ 2010-05-16 17:58Reply to this comment

    等待rmbus

  16. 16Don 评论 @ 2010-05-16 18:17Reply to this comment

    好像adobe也在用8000这个端口

  17. 17bones7456 评论 @ 2010-05-16 18:40Reply to this comment

    @Don: 可以在命令行后面加一个数字参数表示端口的。

  18. 18simon 评论 @ 2010-05-16 18:47Reply to this comment

    如果上传1000个文件,那真杯具了...

  19. 19fcicq 评论 @ 2010-05-16 19:26Reply to this comment

    随意问问, GPLv2/3? 还是别的 License?

  20. 20hmy 评论 @ 2010-05-16 20:00Reply to this comment

    @simon 可以tar的。

  21. 21Leos 评论 @ 2010-05-16 20:08Reply to this comment

    恩,很实用的一个工具……

  22. 22dofine 评论 @ 2010-05-16 21:13Reply to this comment

    在firefox里乱码,chrome里正常. 编码都是utf8啊. 为何?

  23. 23Cheese 评论 @ 2010-05-16 22:02Reply to this comment

    好啊,支持下

  24. 24沈觅仁 评论 @ 2010-05-16 22:25Reply to this comment

    好东西!!体现脚本的强大与方便啊!

  25. 25ebird 评论 @ 2010-05-16 22:48Reply to this comment

    在win下装python貌似比较麻烦哦~怎么说也不如直接启动个hfs方便~

  26. 26bones7456 评论 @ 2010-05-16 22:49Reply to this comment

    @fcicqpython的license是GPL兼容的,我那代码就GPL随便v几吧,反正是在python上修改的,呵呵。

  27. 27Blackdream 评论 @ 2010-05-16 23:54Reply to this comment

    那些等rmbus的。别玩了。玩一会,点到为止还算新鲜。但玩久了,就变得有些无聊。大家都停止了吧。

    感谢这个好东西。

    linux发行版与linux发行版只要简单的scp即可。

    现在windows和linux也可以很简单了。

  28. 28Zer4tul 评论 @ 2010-05-17 3:39Reply to this comment

    赞骨头,收藏了。好东西啊,呵呵。

  29. 29Jian Lee 评论 @ 2010-05-17 9:02Reply to this comment

    不错,很实用!

  30. 30missing 评论 @ 2010-05-17 9:36Reply to this comment

    不得不支持一下下烂骨头~~~

  31. 31ybin.sun 评论 @ 2010-05-17 9:52Reply to this comment

    Great, I can share files with my fellows on Windows from now on. Thank you so much.

  32. 32muxueqz 评论 @ 2010-05-17 10:23Reply to this comment

    支持骨头兄。

  33. 33wang0109 评论 @ 2010-05-17 12:51Reply to this comment

    write a small scp script

  34. 34aweall 评论 @ 2010-05-17 15:06Reply to this comment

    骨头的博客很有趣 收藏了~

  35. 35UNKNOWN 评论 @ 2010-05-19 14:48Reply to this comment

    为什么不用nc,这个感觉更快捷,就是文件多的时候就没办法了.

  36. 36lishuai 评论 @ 2010-05-19 18:08Reply to this comment

    @UNKNOWN: nc确实也很不错,不过对方也需要nc,还要商量下端口什么的,这个也很有优势啊,乱码问题也参照上头的方法搞定了,很不错的传输文件方法,再顶一次!

  37. 37jin 评论 @ 2011-08-30 2:25Reply to this comment

    想改成80端口怎么改啊,老师!

  38. 38喵喵 评论 @ 2011-10-24 11:44Reply to this comment

    @jin: sudo python -m SimpleHTTPServerWithUpload 80

posted @ 2013-09-04 15:08  tangr206  阅读(820)  评论(0)    收藏  举报