Thought this was cool: Python写简单的网络服务器示例
“Life is short, you need Python!”
用Python写一个socket服务有多难?
import socket server = socket.socket() server.bind(("", 8000)) server.listen(1) while True: fd, addr = server.accept() fd.send(str(addr))
测试:
root@xiaoxia-pc:~# telnet xiaoxia 8000
Trying 127.0.0.1…
Connected to xiaoxia.
Escape character is ‘^]’.
(‘127.0.0.1’, 38603)^]
telnet> Connection closed.
用Python写一个HTTP回应服务有多难?
from BaseHTTPServer import * class MyHTTPHandler(BaseHTTPRequestHandler): def do_GET(self): self.wfile.write("Hello world.") server = HTTPServer(("", 8000), MyHTTPHandler) server.serve_forever()
测试:
用Python写一个HTTP资源请求服务有多难?
import SimpleHTTPServer import BaseHTTPServer server = BaseHTTPServer.HTTPServer(("", 8000), SimpleHTTPServer.SimpleHTTPRequestHandler) server.serve_forever()
测试:
这些代码很简短,就实现了很多功能,而且扩展性也很强,
这个,《Python小程序: ServerInformation》
还有这个,《用Python写一个本地Sogou代理服务器程序》
from Xiaoxia[PG]: http://xiaoxia.org/2011/11/03/python-server-examples/
留下评论