-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress.py
149 lines (122 loc) · 3.79 KB
/
progress.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
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
# based on:
# https://medium.com/swlh/build-web-server-from-scratch-with-python-60188f3b162a
import bpy
import socket
from threading import Thread
from time import time, strftime, sleep, localtime, gmtime
import webbrowser
from phaenotyp import basics
class http:
active = False
server = None
address = None
start = b'''\
HTTP/1.1 200 OK
<html>
'''
refresh = b'''\
<meta http-equiv="refresh" content="1">
'''
head = b'''\
<head>
<title>
Phaenotyp | Progress
</title>
'''
style = b'''\
<style>
* {font-family: sans-serif;}
a:link {color: rgb(0, 0, 0); background-color: transparent; text-decoration: none;}
a:visited {color: rgb(0,0,0); background-color: transparent; text-decoration: none;}
a:hover {color: rgb(0,0,0); background-color: transparent; text-decoration: underline;}
a:active {color: rgb(0,0,0); background-color: transparent; text-decoration: underline;}
</style>
'''
headline = b'''\
Phaenotyp | Progress <br>
<br>
'''
@staticmethod
def show_address():
text = '<p>You can access this side in your local network via:<br>'
text += http.address + '</p>'
text += 'Make sure the port is open.</p>'
text += '<br>'
return text.encode()
@staticmethod
def show_terminal():
text = '<p>'
for line in basics.terminal:
text += str(line) + '<br>'
text += '</p>'
text += '<br>'
return text.encode()
@staticmethod
def table_text(first, second):
text = '<table>'
text += '<tr>'
# first column
text += '<td style="width:150">'
text += '<p align="left">' + str(first) + '</p>'
text += '</td>'
# second
text += '<td style="width:100">'
text += '<p align="right">' + str(second) + '</p>'
text += '</td>'
text += '</tr>'
text += '</table>'
return text.encode()
@staticmethod
def setup():
try:
host, port = '', 8888
# only to get ip
import socket
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
listen_socket.connect(("8.8.8.8", 8888))
http.address = str(listen_socket.getsockname()[0]) + ":" + str(port)
listen_socket.close()
# start to access local or in network
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((host, port))
listen_socket.setblocking(0)
listen_socket.listen(1)
http.server = listen_socket
except:
pass
@staticmethod
def hosting():
try:
listen_socket = http.server
# show http
client_connection, client_address = listen_socket.accept()
request_data = client_connection.recv(1024)
# looks like this is necessary with threading?
client_connection.send(b'HTTP/1.0 200 OK')
client_connection.sendall(http.start)
client_connection.sendall(http.refresh)
client_connection.sendall(http.style)
client_connection.sendall(http.headline)
# show ip and port
client_connection.sendall(http.show_address())
# tables
jobs_total = str(basics.jobs_total)
jobs_percentage = str(int(basics.jobs_percentage)) + " %"
time_started = strftime("%H:%M:%S", localtime(basics.time_started))
time_elapsed = strftime("%H:%M:%S", gmtime(basics.time_elapsed))
time_left = strftime("%H:%M:%S", gmtime(basics.time_left))
client_connection.sendall(http.table_text("jobs_total:", jobs_total))
client_connection.sendall(http.table_text("jobs_percentage:", jobs_percentage))
client_connection.sendall(http.table_text("time_started:", time_started))
client_connection.sendall(http.table_text("time_elapsed:", time_elapsed))
client_connection.sendall(http.table_text("time_left:", time_left))
# show terminal
client_connection.sendall(http.show_terminal())
client_connection.close()
except:
pass
def run():
http.active = True
bpy.ops.wm.phaenotyp_webinterface()
webbrowser.open("http://127.0.0.1:8888/")