-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSER.py
More file actions
222 lines (154 loc) · 6.55 KB
/
Copy pathSER.py
File metadata and controls
222 lines (154 loc) · 6.55 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import socket
import threading
import sys
class server:
def __init__ (self): # which is always executed when the class is being initiated.
HOST, PORT = '0.0.0.0', 9000
#https://docs.python.org/2/howto/sockets.html
'''
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), 80))
serversocket.listen(5)
'''
self.folder = 'web'
def start (self):
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # socket library, socket method (pass socket method)
try:
print ('Starting server on port %s ...' % PORT)
#s.connect(("",9000)) #s.connect(addr) makes a connection
s.bind((HOST,PORT)) # only visible within the same machine
# Tuple (() )
#s.bind(('', 80)) specifies that the socket is reachable by any address the machine happens to have.
print ('Server Started on port %s ...' % PORT)
except Exception as e:
print ('Error ! Could not bind to port on %s ...' % PORT)
self.shutdown()
sys.exit(1)
self.incoming() #cheak for incoming requests
def shutdown(self):
try:
print ('Shutting down server')
s.socket.shutdown(socket.SHUT_RDWR) #RD = Read , WR = Write
except Exception as e:
pass #pass this , means port is alredy closed
def makeHedder ():
header = ''
if responceCode == 200:
header = header + 'HTTP/1.1 200 OK\n'
elif responceCode == 404:
header = header + 'HTTP/1.1 404 NOT Found \n'
header = header + 'Connection Closed \n'
return header
def incoming(self):
self.socket.listen(5)
while True:
con,add = self.s.accept()
conThread = threading.Thread(target=self.handler , args=(con,add)) #threading library thread method
conThread.daemon = True #wont end until all the threads are done
conThread.start() #start connetcion
connections.append(con)
print(connections)
'''
def handler (self,con,add):
global connections
while True:
request = con.recv(1024).decode() #data recive 1024 byts
if not request:
connections.remove(con)
con.close()
break #if no data, loop will break
if reqMethod
'''
'''
for connect in connections:
connect.send(bytes(request))
if not request:
connections.remove(con)
con.close()
break #if no data, loop will break
'''
#print ('Serving HTTP on port %s ...' % PORT)
# while 1: # I think python V-2
'''
# accept connections from outside
(clientsocket, address) = serversocket.accept()
# now do something with the clientsocket
# in this case, we'll pretend this is a threaded server
ct = client_thread(clientsocket)
ct.run()
'''
#s.settimeout(10.0) #optional timeout
#s.settimeout(None)
# connections =[] #for connections empty list
def handler (con,add):
global connections
while True:
request = con.recv(1024) #data recive 1024 byts
for connect in connections:
connect.send(bytes(request))
if not request:
connections.remove(con)
con.close()
break #if no data, loop will break
print (request)
if request_method == "GET" or request_method == "HEAD":
# "GET /index.html" split on space
file_requested = data.split(' ')[1]
# If get has parameters ('?'), ignore them
#"GET /index.html?id=11" split on space
file_requested = file_requested.split('?')[0]
if file_requested == "/":
file_requested = "/index.html"
filepath_to_serve = self.folder + file_requested
print("Serving web page [{fp}]".format(fp=filepath_to_serve))
# Load and Serve files content
try:
f = open(filepath_to_serve, 'rb')
if request_method == "GET": # Read only for GET
response_data = f.read()
f.close()
response_header = self.makeHedder(200)
except Exception as e:
print("File not found. Serving 404 page.")
response_header = self.makeHedder(404)
if request_method == "GET": # Temporary 404 Response Page
response_data = b"<html><body><center><h1>Error 404: File not found</h1></center><p>Head back to <a href="/">dry land</a>.</p></body></html>"
response = response_header.encode()
if request_method == "GET":
response += response_data
client.send(response)
client.close()
break
else:
print("Unknown HTTP request method: {method}".format(method=request_method))
# con = connection socket , a = address
'''
while True:
con,add = s.accept()
conThread = threading.Thread(target=handler , args=(con,add)) #threading library thread method
conThread.daemon = True #wont end until all the threads are done
conThread.start() #start connetcion
connections.append(con)
print(connections)
request = con.recv(1024)
print (request)
'''
"""
c.sendall(http_response) #can use send()
c.close()
'''
s.send("GET /index.html HTTP/1.0\n\n") #send request
data = s.recv(10000) # get request
s.close() #shuts down the connection
'''
#s.gethostname()
fragments = []
while not done :
chunk = s.recv(1024)
if not chunk:
break
fragments.append(chunk)
message = " ".join(fragments)
"""
server(start)