โค้ด: เลือกทั้งหมด
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
sql3 = "SELECT * FROM des_ur WHERE ur_id= '%s' " % (id1)
sql3 = sql3.encode('utf-8')
cur.execute(sql3)
rows = cur.fetchall()
total = len(rows)
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='bootstrap4')
print(rows[offset: offset + per_page])
return render_template('inputurl.html',
rows=rows[offset: offset + per_page],
row=row,
page=page,
per_page=per_page,
pagination=pagination)
โค้ด: เลือกทั้งหมด
def post(self,r=None,s=None):
try:
con = p.connect(host='127.0.0.1', database='test', user='postgres', password='123')
cur = con.cursor()
URL = request.form['url']
req = Request(URL, headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})
urlinfo = urlparse(URL)
start = time.time()
ip = socket.gethostbyname(urlinfo.netloc)
dns_tm = time.time() - start
print('DNS:\t\t{:.3f} seconds'.format(dns_tm))
start = time.time()
html = str(urlopen(req).read())
load_tm = time.time() - start
print('load:\t\t{:.3f} seconds'.format(load_tm))
print('w/o DNS:\t{:.3f} seconds'.format(load_tm - dns_tm))
sql = """INSERT INTO ur (url,dns,load_ur,wo_dns)
VALUES ('%s','%s','%s','%s') RETURNING id;""" % (
URL, dns_tm, load_tm, load_tm - dns_tm)
sql = sql.encode('utf-8')
cur.execute(sql)
id1 = str(cur.fetchone()[0])
con.commit()
soup = BeautifulSoup(html, "html.parser")
for link in soup.find_all('a', href=re.compile('^https')):
print(link.get('href'))
urlinfo2 = urlparse(link.get('href'))
req2 = Request(link.get('href'), headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})
start = time.time()
ip = socket.gethostbyname(urlinfo2.netloc)
dns_tm2 = time.time() - start
print('DNS:\t\t{:.3f} seconds'.format(dns_tm2))
start = time.time()
html = str(urlopen(req2).read())
load_tm2 = time.time() - start
print('load:\t\t{:.3f} seconds'.format(load_tm2))
print('w/o DNS:\t{:.3f} seconds'.format(load_tm2 - dns_tm2))
# time.sleep(1)
url = str(link.get('href'))
sql2 = "SELECT * FROM des_ur WHERE url = '%s' AND ur_id= '%s' " % (url, id1)
sql2 = sql2.encode('utf-8')
cur.execute(sql2)
check = cur.fetchall()
if check:
pass
else:
sql5 = """INSERT INTO des_ur (url,dns,load_ur,wo_dns,ur_id)
VALUES ('%s','%s','%s','%s','%s')""" % (
url, dns_tm2, load_tm2, load_tm2 - dns_tm2, id1)
sql5 = sql5.encode('utf-8')
cur.execute(sql5)
con.commit()
sql4 = "SELECT * FROM ur WHERE id= '%s' " % (id1)
sql4 = sql4.encode('utf-8')
cur.execute(sql4)
row = cur.fetchall()
print(row)
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
sql3 = "SELECT * FROM des_ur WHERE ur_id= '%s' " % (id1)
sql3 = sql3.encode('utf-8')
cur.execute(sql3)
rows = cur.fetchall()
total = len(rows)
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='bootstrap4')
print(rows[offset: offset + per_page])
return render_template('inputurl.html',
rows=rows[offset: offset + per_page],
row=row,
page=page,
per_page=per_page,
pagination=pagination)
except Exception as e:
current_app.logger.info(e)
response = 'error'
return str(e)
ส่วน code นี้ เขียนเหมือนกันแต่ใช้ได้ปกติ
โค้ด: เลือกทั้งหมด
@app.route('/')
def index():
con = p.connect(host='127.0.0.1', database='test', user='postgres', password='123')
cur = con.cursor()
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
sql4 = "SELECT * FROM ur "
sql4 = sql4.encode('utf-8')
cur.execute(sql4)
rows = cur.fetchall()
total = len(rows)
# pagination_users = get_users(offset=offset, per_page=per_page)
pagination = Pagination(page=page, per_page=per_page, total=total,
css_framework='bootstrap4')
return render_template('test.html',
users=rows[offset: offset + per_page],
page=page,
per_page=per_page,
pagination=pagination,
)
codeแรก
codeสอง