-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlCodeRunner-LocalHostOpner.c
67 lines (60 loc) · 1.36 KB
/
HtmlCodeRunner-LocalHostOpner.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/types.h>
#endif
int main() {
char *command = (char *)malloc(sizeof(char) * 100);
bool serverStarted = false;
// Start Python HTTP server in the background
#ifdef _WIN32
strcpy(command, "start /B python -m http.server");
#else
strcpy(command, "python -m http.server &");
#endif
system(command);
serverStarted = true;
system("cls");
// Wait for a short period to ensure the server starts
#ifdef _WIN32
Sleep(2000);
#else
sleep(2);
#endif
// Open the browser to the localhost address
#ifdef _WIN32
strcpy(command, "start http://localhost:8000");
#else
strcpy(command, "xdg-open http://localhost:8000 &");
#endif
system(command);
system("cls");
// Wait for user input to exit
printf("Press Enter to exit.\n");
getchar();
// Close the Python server gracefully
#ifdef _WIN32
strcpy(command, "taskkill /f /im python.exe");
#else
strcpy(command, "pkill -f 'python -m http.server'");
#endif
system(command);
system("cls");
// Wait for the Python server to terminate
if (serverStarted) {
#ifdef _WIN32
Sleep(2000);
#else
sleep(2);
#endif
}
system("cls");
system("cls");
free(command);
return 0;
}