-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst.py
120 lines (80 loc) · 3.25 KB
/
first.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from flask import Flask,jsonify,json,make_response,request,redirect, url_for,abort,render_template
import requests
app=Flask(__name__)
#------------------------------TASK 1-------------------------------------
#main page or index
@app.route('/')
def index():
return 'HelloWorld - Rohan' #return simple string
#------------------------------TASK 2-------------------------------------
@app.route('/authors')
def hi():
#---------------------TASK 2a--------------------
#make request to url
resForAuthor = requests.get('https://jsonplaceholder.typicode.com/users')
#convert json to array(javascript object)
objForAuthor = resForAuthor.json()
#finding length of array
lengthOfAuthors=len(objForAuthor)
#---------------------TASK 2b--------------------
#make request to url
resForPost=requests.get('https://jsonplaceholder.typicode.com/posts')
#convert json to array(javascript object)
objForPost=resForPost.json()
#finding length of array
lengthOfPosts=len(objForPost)
#---------------------TASK 2c--------------------
post="Author Name : Number Of Posts</br>"
#iternate over every author
for i in range(lengthOfAuthors):
x=0
post+=objForAuthor[i]["name"]+' : '
#iterate over every post
for j in range(lengthOfPosts):
#check if a post is wirtten by a specific author(id)
if objForAuthor[i]["id"]==objForPost[j]["userId"]:
x=x+1
post+=str(x)+'</br>'
return(post)
#------------------------------TASK 3-------------------------------------
@app.route('/setcookie')
def setcookie():
#getting reasponse from client by sending string to a client
resp = make_response('cookie is set for name and age')
#set cookie corresponding to name
resp.set_cookie('name', 'rohan')
#set cookie corresponding to age
resp.set_cookie('age', '22')
return resp
#------------------------------TASK 4-------------------------------------
@app.route('/getcookies')
def getcookie():
#if cookie is pesent then retrieve cookie from their key
cookie=request.cookies.get('name') + '</br>' + request.cookies.get('age')
return cookie
#------------------------------TASK 5-------------------------------------
@app.route('/robots.txt')
def deny():
#sending unauthorize acess to client
abort(401)
return 'You donot have enough privilidge'
#------------------------------TASK 6-------------------------------------
@app.route('/html')
def image():
#sending html string pesent /templates/main.html file to client
return render_template('main.html')
#------------------------------TASK 7-------------------------------------
@app.route('/input')
def input():
#sending input.html string to client
return render_template('input.html')
@app.route('/getinput', methods=['GET','Post'])
def getinput():
#check if request is dony POST method
if request.method=='POST':
#log the text written in input box
print(request.form['textbox'])
#redirect to /input page
return redirect(url_for('input'))
if __name__=="__main__":
app.run(debug=True)