Concurrence系列-HTTP

服务端

 

from concurrence import dispatch from concurrence.http import WSGIServer def hello_world(environ, start_response): start_response("200 OK", []) return ["<html>Hello, world!</html>"] def main(): server = WSGIServer(hello_world) server.serve(('localhost', 8080)) if __name__ == '__main__': dispatch(main) 

 

 

客户端

 

 

发送一个请求:

from concurrence import dispatch from concurrence.http import HTTPConnection def main(): cnn = HTTPConnection() cnn.connect(('www.google.com', 80)) request = cnn.get('/') response = cnn.perform(request) print response.status print response.headers print response.body cnn.close() if __name__ == '__main__': dispatch(main) 

 

 

使用Pipelining完成多个请:

from concurrence import dispatch from concurrence.http import HTTPConnection def main(): cnn = HTTPConnection() cnn.connect(('www.google.com', 80)) request = cnn.get('/') #you can send multiple http requests on the same connection: cnn.send(request) #request 1 cnn.send(request) #request 2 #and receive the corresponding responses response1 = cnn.receive() response2 = cnn.receive() print response1.status print response1.headers print response1.body print response2.status print response2.headers print response2.body cnn.close() if __name__ == '__main__': dispatch(main) 

 

 

你可能感兴趣的:(server,import)