#!/usr/bin/env python
# coding=utf8
# si.hairui, 2016.11.24
# Http Client, Python 2.7.5
import httplib
# 服务器信息
HTTP_SERVER_LIST = [
{'ip': "192.168.0.235", 'port': 80, 'filepath': "/web/video/smartdeer.mp4"},
{'ip': "192.168.0.236", 'port': 80, 'filepath': "/web/video/toystory.mp4"}
]
# 客户端信息列表
HTTP_CLIENT_LIST = [
{'ip': "192.168.0.218", 'port': 7575},
{'ip': "192.168.0.219", 'port': 7575},
{'ip': "192.168.0.220", 'port': 2434},
{'ip': "192.168.0.221", 'port': 2434}
]
# 轮询服务器和客户端发起HTTP请求
for httpServer in HTTP_SERVER_LIST:
for httpClient in HTTP_CLIENT_LIST:
try:
myHttpConn = httplib.HTTPConnection(httpServer['ip'], httpServer['port'],
source_address=(httpClient['ip'], httpClient['port']))
myHttpConn.debuglevel = 0 # 调试模式开关
myHttpConn.timeout = 3 # 设置连接超时等待(单位:s)
if 0 == myHttpConn.debuglevel:
print "---- Http Request ----"
print httpClient['ip'] + ":", httpClient['port'], \
" --> " + httpServer['ip'], httpServer['port'], ": " + httpServer['filepath']
myHttpConn.request("GET", httpServer['filepath']) # 资源路径以“/”开始
myHttpResp = myHttpConn.getresponse()
if 0 == myHttpConn.debuglevel:
print myHttpResp.status, myHttpResp.reason # 响应码和状态信息
# 此处必须读响应内容,如果不读取,buff将溢出!!!
data1 = myHttpResp.read()
pass
except:
print "Exception!"
finally:
# print data1 # 打印响应内容
print "Http(%s:%s --> %s:%s) will be closed!" % \
(httpClient['ip'], str(httpClient['port']), httpServer['ip'], str(httpServer['port']))
myHttpConn.close() # 关闭连接
Python Http 多客户端和服务器(串行)
最新推荐文章于 2024-04-29 14:36:18 发布