-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
177 lines (83 loc) · 2.98 KB
/
train.py
File metadata and controls
177 lines (83 loc) · 2.98 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import socket,mysql.connector
import id_
def db_connection():
conn=mysql.connector.connect(
host='localhost',
user='root',
password='your_password',
database='train'
)
return conn
def extract_body_from_request(request):
body=[]
request=request.split('\n')
# print("here\n",request)
i=len(request)-1
while i>=0 and request[i]!='\r':
body.insert(0,request[i])
i-=1
x=body[0].split('&')
name=x[0][5::].replace('+',' ')
password=x[1][5::].replace('+',' ')
return name,password
server_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_skt.bind(('localhost', 5000))
server_skt.listen(5)
index_file=""
with open("index.html",'r') as f:
index_file=f.read()
index_response=html_response = f"""HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n
{index_file}
""".encode()
create_account=""
with open("create_account.html",'r') as f:
create_account=f.read()
account_response=f"""HTTP/1.1 200 OK \r\nContent-Type: text/html\r\n\r\n
{create_account}
""".encode()
insert_response=f"""HTTP/1.1 200 OK \r\nContent-Type: text/plane\r\n\r\n
done
""".encode()
check_response1=f"""HTTP/1.1 200 OK \r\nContent-Type: text/plane\r\n\r\n
exist
""".encode()
check_response0=f"""HTTP/1.1 200 OK \r\nContent-Type: text/plane\r\n\r\n
not exist
""".encode()
while True:
# moving the connection from the accept q of the socket to be a independent entity (connection)
client_skt, client_address = server_skt.accept()
print(f"Connection accepted from {client_address}")
request=client_skt.recv(1024).decode()
route,method="",""
if len(request):
method=request.split()[0]
route=request.split()[1]
if method=='GET' and (route=='/' or route=='/index'):
client_skt.send(index_response)
elif method=='GET' and route=='/create_account':
client_skt.send(account_response)
elif method=="POST" and route=="/insert":
name,password=extract_body_from_request(request)
# insert in the data base
conn=db_connection()
cursor=conn.cursor()
query="insert into info (id,name,pass) values (%s,%s,%s)"
res=cursor.execute(query,(id_.id,name,password))
id_.id+=1
conn.commit()
print("res is ",res)
client_skt.send(insert_response)
cursor.close()
conn.close()
elif method=='POST' and route=='/check':
name,password=extract_body_from_request(request)
conn=db_connection()
cursor=conn.cursor()
query="select * from info where name=%s and pass=%s"
cursor.execute(query,(name,password))
res=cursor.fetchall()
if len(res):
client_skt.send(check_response1)
else:
client_skt.send(check_response0)