如何用Python写一个http post请求
1个回答
展开全部
你运气真好,我这刚好有现成的,下面大写的常量就是一些字符串,涉及我们自己的协议,就不发你了,你随便写个就行
def RequestCenter( data,url,web="127.0.0.1" ,port=9228 ):
headDic = {}
headDic[REQUEST_HEAD_LENGTH] = len( data )
tempList = []
tempList.append( headDic )
tempList.extend( data )
# 远程访问
count = 0
jsonData = json.dumps( tempList )
headers = {"Content-type": "application/json"}
state = -1
while count < 1:
com = httplib.HTTPConnection( web,port,True )
com.request( "POST",url,jsonData,headers )
response = com.getresponse()
time.sleep( 0.5 )
infoList = []
state = response.status
if 200 <= state < 300:
infoList = json.loads( response.read() )
com.close()
break
else:
count += 1
com.close()
return infoList
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询