python3编写发送四种http请求的脚本

时间:2024-10-30 11:06:20

python3编写发送http请求的脚本

使用requests包:

http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

使用urllib包:

https://www.jianshu.com/p/9e50c58dabdd

https://blog.****.net/u010211479/article/details/66475183

import requests

def get(url):
res=requests.get(url)
result=res.text
print(result)

def post(url,querystr):
data=eval(querystr)
res=requests.post(url,data=data)
result=res.text
print(result)

def put(url,querystr):
if querystr!=None and querystr!="":
data=eval(querystr)
res=requests.put(url,data=data)
result=res.text
print(result)
res=requests.put(url)
result=res.text
print(result)

def delete(url,querystr):
res=requests.delete(url)
result=res.text
print(result)

if __name__=="__main__":
#host=input("host&port:")
host="139.199.32.101:9200"
path=input("path:")
url="http://"+host+"/"+path
print(url)
type=input("type:")

str=""
with open("querystr.txt") as file:
str=file.read().strip()

if type=="get":
get(url)
elif type=="post":
post(url,str)
elif type=="put":
put(url,str)
elif type=="delete":
delete(url,str)