Last active 1722542665

kristofer's Avatar kristofer revised this gist 1722542665. Go to revision

1 file changed, 13 insertions

client.py(file created)

@@ -0,0 +1,13 @@
1 + # sample http client in python
2 + import http.client
3 +
4 + try:
5 + connection = http.client.HTTPConnection("localhost:8000")
6 + connection.request("GET", "/foo")
7 + response = connection.getresponse()
8 + print(response)
9 + print("Status: {} and result: {}".format(response.status, response.read()))
10 + connection.close()
11 + except Exception as e:
12 + print("Error: {}".format(e))
13 +

kristofer's Avatar kristofer revised this gist 1722542621. Go to revision

1 file changed, 22 insertions

SimpleHTTPServer.java(file created)

@@ -0,0 +1,22 @@
1 + package rocks.zipcode;
2 +
3 + import java.io.IOException;
4 + import java.net.ServerSocket;
5 + import java.net.Socket;
6 + import java.util.Date;
7 +
8 +
9 + // Show how Sockets work. Lower level than Main class.
10 + class SimpleHTTPServer {
11 + public static void main(String args[]) throws IOException {
12 + ServerSocket server = new ServerSocket(8001);
13 + System.out.println("Listening for connection on port 8080 ....");
14 + while (true) {
15 + try (Socket socket = server.accept()) {
16 + Date today = new Date();
17 + String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
18 + socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
19 + }
20 + }
21 + }
22 + }

kristofer's Avatar kristofer revised this gist 1722542586. Go to revision

1 file changed, 1 insertion, 29 deletions

ZipWebServer.java

@@ -100,35 +100,7 @@ public class ZipWebServer {
100 100 // sb.append("<p>SELECT * FROM users WHERE username = 'admin';</p>");
101 101
102 102 sb.append("<h3>Header Level 3</h3>");
103 - spackage rocks.zipcode;
104 -
105 - import java.io.IOException;
106 - import java.io.InputStream;
107 - import java.net.URL;
108 - import java.net.URLConnection;
109 - import java.util.Scanner;
110 -
111 - public class WebClient {
112 - public static void main(String[] args) throws IOException {
113 - String url = "http://localhost:8000";
114 - if (args.length == 0) {
115 - Scanner scanner = new Scanner(System.in);
116 - System.out.print("Enter a URL: ");
117 - url = scanner.nextLine();
118 - } else {
119 - url = args[0];
120 - }
121 - try {
122 - URLConnection connection = new URL(url).openConnection();
123 - InputStream response = connection.getInputStream();
124 - Scanner scanner = new Scanner(response);
125 - String responseBody = scanner.useDelimiter("\\A").next();
126 - System.out.println(responseBody);
127 - } catch (IOException e) {
128 - System.out.println("Error fetching URL: " + e.getMessage());
129 - }
130 - }
131 - }b.append("<p>");
103 + sb.append("<p>");
132 104 for(int i = 0; i<5; i++) {
133 105 sb.append("Experience is the teacher of all things.\n");
134 106 }

kristofer's Avatar kristofer revised this gist 1722542532. Go to revision

2 files changed, 171 insertions

WebClient.java(file created)

@@ -0,0 +1,29 @@
1 + package rocks.zipcode;
2 +
3 + import java.io.IOException;
4 + import java.io.InputStream;
5 + import java.net.URL;
6 + import java.net.URLConnection;
7 + import java.util.Scanner;
8 +
9 + public class WebClient {
10 + public static void main(String[] args) throws IOException {
11 + String url = "http://localhost:8000";
12 + if (args.length == 0) {
13 + Scanner scanner = new Scanner(System.in);
14 + System.out.print("Enter a URL: ");
15 + url = scanner.nextLine();
16 + } else {
17 + url = args[0];
18 + }
19 + try {
20 + URLConnection connection = new URL(url).openConnection();
21 + InputStream response = connection.getInputStream();
22 + Scanner scanner = new Scanner(response);
23 + String responseBody = scanner.useDelimiter("\\A").next();
24 + System.out.println(responseBody);
25 + } catch (IOException e) {
26 + System.out.println("Error fetching URL: " + e.getMessage());
27 + }
28 + }
29 + }

ZipWebServer.java(file created)

@@ -0,0 +1,142 @@
1 + package rocks.zipcode;
2 +
3 + import java.io.IOException;
4 + import java.io.OutputStream;
5 + import java.net.InetSocketAddress;
6 +
7 + import com.sun.net.httpserver.HttpExchange;
8 + import com.sun.net.httpserver.HttpHandler;
9 + import com.sun.net.httpserver.HttpServer;
10 +
11 + public class ZipWebServer {
12 + static final int port = 8000;
13 +
14 + public static void main(String[] args) throws Exception {
15 + HttpServer server = HttpServer.create(new InetSocketAddress(ZipWebServer.port), 0);
16 + server.createContext("/", new RootHandler());
17 + server.createContext("/test", new TestHandler());
18 + server.createContext("/foo", new FooHandler());
19 + server.createContext("/bar", new BarHandler());
20 + // server.setExecutor(null); // creates a default executor
21 + // server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
22 + server.setExecutor(java.util.concurrent.Executors.newFixedThreadPool(5));
23 + server.start();
24 + }
25 +
26 + static class RootHandler implements HttpHandler {
27 + @Override
28 + public void handle(HttpExchange t) throws IOException {
29 + if (t.getRequestMethod().equals("GET") ) {
30 + String response = "Zip Code Rocks!";
31 + t.sendResponseHeaders(200, response.length());
32 + OutputStream os = t.getResponseBody();
33 + os.write(response.getBytes());
34 + os.close();
35 + } else {
36 + String response = "No GET!";
37 + t.sendResponseHeaders(404, response.length());
38 + OutputStream os = t.getResponseBody();
39 + os.write(response.getBytes());
40 + os.close();
41 + }
42 + }
43 + }
44 + static class TestHandler implements HttpHandler {
45 + @Override
46 + public void handle(HttpExchange t) throws IOException {
47 + String response = "Testing! Testing! Zip Code Rocks!";
48 + t.sendResponseHeaders(200, response.length());
49 + OutputStream os = t.getResponseBody();
50 + os.write(response.getBytes());
51 + os.close();
52 + }
53 + }
54 + static class FooHandler implements HttpHandler {
55 + @Override
56 + public void handle(HttpExchange t) throws IOException {
57 + String response = "<HTML><BODY><h1>HTML</h1><p>This is a proper paragraph of text.</p></BODY></HTML>";
58 + t.sendResponseHeaders(200, response.length());
59 + OutputStream os = t.getResponseBody();
60 + os.write(response.getBytes());
61 + os.close();
62 + }
63 + }
64 +
65 + static class BarHandler implements HttpHandler {
66 + @Override
67 + public void handle(HttpExchange t) throws IOException {
68 + String response = MakeBody();
69 + t.sendResponseHeaders(200, response.length());
70 + OutputStream os = t.getResponseBody();
71 + os.write(response.getBytes());
72 + os.close();
73 + }
74 + }
75 +
76 + static String MakeBody() {
77 + StringBuilder sb = new StringBuilder();
78 + String css = "body { font-family: Arial, sans-serif; line-height: 1.6; color: #222; " +
79 + "max-width: 40rem; padding: 2rem; margin: auto; background: #fafafa; }" +
80 + "a { color: #2ECC40; } h1, h2, strong { color: #111; }";
81 +
82 + sb.append("<HTML>");
83 + sb.append("<style>");
84 + sb.append(css);
85 + sb.append("</style>");
86 + sb.append("<BODY><h1>Level One Header</h1>");
87 + sb.append("<p>");
88 + for(int i = 0; i<10; i++) {
89 + sb.append("This is a proper paragraph of text. ");
90 + }
91 + sb.append("</p>");
92 + sb.append("<h2>Header Level 2</h2>");
93 + sb.append("<p><em>");
94 + for(int i = 0; i<2; i++) {
95 + sb.append("How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth? We know that he did not come through the door, the window, or the chimney. We also know that he could not have been concealed in the room, as there is no concealment possible. When, then, did he come?. <br/> ");
96 + }
97 + sb.append("</em></p>");
98 +
99 + // do a bunch SQL queries here
100 + // sb.append("<p>SELECT * FROM users WHERE username = 'admin';</p>");
101 +
102 + sb.append("<h3>Header Level 3</h3>");
103 + spackage rocks.zipcode;
104 +
105 + import java.io.IOException;
106 + import java.io.InputStream;
107 + import java.net.URL;
108 + import java.net.URLConnection;
109 + import java.util.Scanner;
110 +
111 + public class WebClient {
112 + public static void main(String[] args) throws IOException {
113 + String url = "http://localhost:8000";
114 + if (args.length == 0) {
115 + Scanner scanner = new Scanner(System.in);
116 + System.out.print("Enter a URL: ");
117 + url = scanner.nextLine();
118 + } else {
119 + url = args[0];
120 + }
121 + try {
122 + URLConnection connection = new URL(url).openConnection();
123 + InputStream response = connection.getInputStream();
124 + Scanner scanner = new Scanner(response);
125 + String responseBody = scanner.useDelimiter("\\A").next();
126 + System.out.println(responseBody);
127 + } catch (IOException e) {
128 + System.out.println("Error fetching URL: " + e.getMessage());
129 + }
130 + }
131 + }b.append("<p>");
132 + for(int i = 0; i<5; i++) {
133 + sb.append("Experience is the teacher of all things.\n");
134 + }
135 + sb.append("</p>");
136 +
137 + sb.append("</BODY></HTML>");
138 +
139 + return sb.toString();
140 + }
141 +
142 + }
Newer Older