Python Webserver
Vamos a ver como lanzar un sencillo webserver con tan solo ejecutar un comando.
Esto puede ser muy util para comprobar rutas , paginas conexiones web etc.
Nota: No lo recomiento en un entorno produtivo de alto rendimiento.
El comando es facil: python3 -m http.server 13020
Donde 13020 es el puerto en el que quiero que escuche.
root@em50l:~# python3 -m http.server 13020 Serving HTTP on 0.0.0.0 port 13020 ...
Cuando un cliente se conecte veremos una linea con la conexion:
127.0.0.1 - - [06/Dec/2019 12:36:53] "GET / HTTP/1.1" 200 - 9.4.198.26 - - [06/Dec/2019 12:37:09] "GET / HTTP/1.1" 200 - ^C Keyboard interrupt received, exiting.
Comprobar que funciona. Probamos desde localhost:
jejo@em50l:~$ curl -kvs localhost:13020 * Trying ::1... * TCP_NODELAY set * connect to ::1 port 13020 failed: Connection refused * Trying 127.0.0.1... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 13020 (#0) > GET / HTTP/1.1 > Host: localhost:13020 > User-Agent: curl/7.52.1 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 200 OK < Server: SimpleHTTP/0.6 Python/3.5.3 < Date: Fri, 06 Dec 2019 11:36:53 GMT < Content-type: text/html; charset=ascii < Content-Length: 1038 < <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ascii"> <title>Directory listing for /</title> </head> <body> <h1>Directory listing for /</h1> <hr> <ul> <li><a href="test.html">test.html</a></li> </ul> <hr> </body> </html> * Curl_http_done: called premature == 0 * Closing connection 0
Apendice Depuracion tcpdum.
Si algo falla es bueno tener informacion tcpdump.
Paquetes/solicitudes que no entran, Respuestas que no salen etc….
Nos puede ayudar a detectar problemas con la red y con la comunicacion TCP/IP.
tcpdump -vvlnn inbound and dst port 13020 or outbound and src port 13020
Si tcpdump es incapaz de detectar el protocolo http tendremos que añadir -X para ver los datos.
tcpdump -vvlnnX inbound and dst port 13020 or outbound and src port 13020
Nota: el filtro es importante. (inbound and dst port 13020 or outbound and src port 13020
)
ya que si nuestro servidor saldria mucho “ruido” que tendriamos que separar.
Aqui dejo una captura de trafico en la que se ven las peticiones y las respuestas HTTP:
root@em50l:# tcpdump -lnnAX -s256 " (outbound and src port 13020) or (inbound and dst port 13020)" tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on venet0, link-type LINUX_SLL (Linux cooked), capture size 256 bytes 12:31:25.758796 IP 94.36.158.56.60270 > 172.16.20.130.13020: Flags [S], seq 494947881, win 27200, options [mss 1308,sackOK,TS val 291161489 ecr 0,nop,wscale 7], length 0 0x0000: 4500 003c 4550 4000 3906 1691 5f2e c61a E..<EP@.9..._... 0x0010: ac10 1482 eb6e 32dc 1d80 4e29 0000 0000 .....n2...N).... 0x0020: a002 6a40 979c 0000 0204 051c 0402 080a ..j@............ 0x0030: 115a c591 0000 0000 0103 0307 .Z.......... 12:31:25.758912 IP 172.16.20.130.13020 > 94.36.158.56.60270: Flags [S.], seq 3146976960, ack 494947882, win 14480, options [mss 1460,sackOK,TS val 3291355375 ecr 291161489,nop,wscale 7], length 0 0x0000: 4500 003c 0000 4000 4006 54e1 ac10 1482 E..<..@.@.T..... 0x0010: 5f2e c61a 32dc eb6e bb93 0ec0 1d80 4e2a _...2..n......N* 0x0020: a012 3890 2132 0000 0204 05b4 0402 080a ..8.!2.......... 0x0030: c42e 18ef 115a c591 0103 0307 .....Z...... 12:31:25.862847 IP 94.36.158.56.60270 > 172.16.20.130.13020: Flags [.], ack 1, win 213, options [nop,nop,TS val 291161608 ecr 3291355375], length 0 0x0000: 4500 0034 4551 4000 3906 1698 5f2e c61a E..4EQ@.9..._... 0x0010: ac10 1482 eb6e 32dc 1d80 4e2a bb93 0ec1 .....n2...N*.... 0x0020: 8010 00d5 8742 0000 0101 080a 115a c608 .....B.......Z.. 0x0030: c42e 18ef .... 12:31:25.862860 IP 94.36.158.56.60270 > 172.16.20.130.13020: Flags [P.], seq 1:85, ack 1, win 213, options [nop,nop,TS val 291161609 ecr 3291355375], length 84 0x0000: 4500 0088 4552 4000 3906 1643 5f2e c61a E...ER@.9..C_... 0x0010: ac10 1482 eb6e 32dc 1d80 4e2a bb93 0ec1 .....n2...N*.... 0x0020: 8018 00d5 14a1 0000 0101 080a 115a c609 .............Z.. 0x0030: c42e 18ef 4745 5420 2f6b 6b20 4854 5450 ....GET./kk.HTTP 0x0040: 2f31 2e31 0d0a 486f 7374 3a20 6861 7264 /1.1..Host: 0x0050: 2e6a 656a 6f2e 7077 3a31 3330 3230 0d0a jejo.pw:13020.. 0x0060: 5573 6572 2d41 6765 6e74 3a20 6375 726c User-Agent:.curl 0x0070: 2f37 2e35 382e 300d 0a41 6363 6570 743a /7.58.0..Accept: 0x0080: 202a 2f2a 0d0a 0d0a .**.... 12:31:25.862986 IP 172.16.20.130.13020 > 94.36.158.56.60270: Flags [.], ack 85, win 114, options [nop,nop,TS val 3291355479 ecr 291161609], length 0 0x0000: 4500 0034 1796 4000 4006 3d53 ac10 1482 E..4..@.@.=S.... 0x0010: 5f2e c61a 32dc eb6e bb93 0ec1 1d80 4e7e _...2..n......N~ 0x0020: 8010 0072 86e8 0000 0101 080a c42e 1957 ...r...........W 0x0030: 115a c609 .Z.. 12:31:25.863949 IP 172.16.20.130.13020 > 94.36.158.56.60270: Flags [P.], seq 1:185, ack 85, win 114, options [nop,nop,TS val 3291355480 ecr 291161609], length 184 0x0000: 4500 00ec 1797 4000 4006 3c9a ac10 1482 E.....@.@.<..... 0x0010: 5f2e c61a 32dc eb6e bb93 0ec1 1d80 4e7e _...2..n......N~ 0x0020: 8018 0072 4c33 0000 0101 080a c42e 1958 ...rL3.........X 0x0030: 115a c609 4854 5450 2f31 2e30 2034 3034 .Z..HTTP/1.0.404 0x0040: 2046 696c 6520 6e6f 7420 666f 756e 640d .File.not.found. 0x0050: 0a53 6572 7665 723a 2053 696d 706c 6548 .Server:.SimpleH 0x0060: 5454 502f 302e 3620 5079 7468 6f6e 2f33 TTP/0.6.Python/3 0x0070: 2e35 2e33 0d0a 4461 7465 3a20 5375 6e2c .5.3..Date:.Sun, 0x0080: 2030 3820 4465 6320 3230 3139 2031 313a .08.Dec.2019.11: 0x0090: 3331 3a32 3520 474d 540d 0a43 6f6e 6e65 31:25.GMT..Conne 0x00a0: 6374 696f 6e3a 2063 6c6f 7365 0d0a 436f ction:.close..Co 0x00b0: 6e74 656e 742d 5479 7065 3a20 7465 7874 ntent-Type:.text 0x00c0: 2f68 746d 6c3b 6368 6172 7365 743d 7574 /html;charset=ut 0x00d0: 662d 380d 0a43 6f6e 7465 6e74 2d4c 656e f-8..Content-Len 0x00e0: 6774 683a 2034 3639 0d0a 0d0a gth:.469.... 12:31:25.864043 IP 172.16.20.130.13020 > 94.36.158.56.60270: Flags [FP.], seq 185:654, ack 85, win 114, options [nop,nop,TS val 3291355480 ecr 291161609], length 469 0x0000: 4500 0209 1798 4000 4006 3b7c ac10 1482 E.....@.@.;|.... 0x0010: 5f2e c61a 32dc eb6e bb93 0f79 1d80 4e7e _...2..n...y..N~ 0x0020: 8019 0072 2111 0000 0101 080a c42e 1958 ...r!..........X 0x0030: 115a c609 3c21 444f 4354 5950 4520 4854 .Z..<!DOCTYPE.HT 0x0040: 4d4c 2050 5542 4c49 4320 222d 2f2f 5733 ML.PUBLIC."-//W3 0x0050: 432f 2f44 5444 2048 544d 4c20 342e 3031 C//DTD.HTML.4.01 0x0060: 2f2f 454e 220a 2020 2020 2020 2020 2268 //EN"........."h 0x0070: 7474 703a 2f2f 7777 772e 7733 2e6f 7267 ttp://www.w3.org 0x0080: 2f54 522f 6874 6d6c 342f 7374 7269 6374 /TR/html4/strict 0x0090: 2e64 7464 223e 0a3c 6874 6d6c 3e0a 2020 .dtd">.<html>... 0x00a0: 2020 3c68 6561 643e 0a20 2020 2020 2020 ..<head>........ 0x00b0: 203c 6d65 7461 2068 7474 702d 6571 7569 .<meta.http-equi 0x00c0: 763d 2243 6f6e 7465 6e74 2d54 7970 6522 v="Content-Type" 0x00d0: 2063 6f6e 7465 6e74 3d22 7465 7874 2f68 .content="text/h 0x00e0: 746d 6c3b 6368 6172 7365 743d 7574 662d tml;charset=utf- 12:31:26.006798 IP 94.36.158.56.60270 > 172.16.20.130.13020: Flags [.], ack 185, win 221, options [nop,nop,TS val 291161733 ecr 3291355480], length 0 0x0000: 4500 0034 4553 4000 3906 1696 5f2e c61a E..4ES@.9..._... 0x0010: ac10 1482 eb6e 32dc 1d80 4e7e bb93 0f79 .....n2...N~...y 0x0020: 8010 00dd 8548 0000 0101 080a 115a c685 .....H.......Z.. 0x0030: c42e 1958 ...X 12:31:26.009713 IP 94.36.158.56.60270 > 172.16.20.130.13020: Flags [F.], seq 85, ack 655, win 230, options [nop,nop,TS val 291161733 ecr 3291355480], length 0 0x0000: 4500 0034 4554 4000 3906 1695 5f2e c61a E..4ET@.9..._... 0x0010: ac10 1482 eb6e 32dc 1d80 4e7e bb93 114f .....n2...N~...O 0x0020: 8011 00e6 8368 0000 0101 080a 115a c685 .....h.......Z.. 0x0030: c42e 1958 ...X 12:31:26.009788 IP 172.16.20.130.13020 > 94.36.158.56.60270: Flags [.], ack 86, win 114, options [nop,nop,TS val 3291355626 ecr 291161733], length 0 0x0000: 4500 0034 1799 4000 4006 3d50 ac10 1482 E..4..@.@.=P.... 0x0010: 5f2e c61a 32dc eb6e bb93 114f 1d80 4e7f _...2..n...O..N. 0x0020: 8010 0072 834a 0000 0101 080a c42e 19ea ...r.J.......... 0x0030: 115a c685 .Z.. ^C 10 packets captured 10 packets received by filter 0 packets dropped by kernel