Python-Ref > Mimo python > HTTP > HTTP pokus v Pythonu
 
 

<-^^
Klíčová slova
Moduly

HTTP pokus v Pythonu

Low-level komunikace s HTTP serverem pomocí Pythonu
Tento krátký skriptík demonstruje jak je možné vlastními silami snadno zkonstruovat HTTP request.
Program používá knihovnu socket, která umožňuje nízkoúrovňovou práci se síťovým rozhraním.
Expand/Shrink
Zdroj: (http3-1.py)
  1   import socket
  2   
  3   # socket creation
  4   soc = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
  5   # we connect the socket to the remote sever on port 80
  6   soc.connect( ("zirael.org", 80))
  7   # we send a plain-text request (ending with an empty line)
  8   soc.send("""GET / HTTP/1.1
  9   Host: zirael.org
 10   
 11   """)
 12   # we read the result, at maximum 2048 bytes in one go
 13   result = soc.recv(2048)
 14   # we display the result..
 15   print result
 16   # ..and close the socket
 17   soc.close()
stdout:
HTTP/1.1 200 OK
Date: Thu, 22 Oct 2009 06:47:17 GMT
Server: Apache
Last-Modified: Tue, 29 Mar 2005 15:09:02 GMT
ETag: "30bef-3e8-3f3750a707f80"
Accept-Ranges: bytes
Content-Length: 1000
Content-Type: text/html; charset=utf-8

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "dtd/xhtml1-strict.dtd">
<?xml version="1.0" ?>
<html>
  <head>
    <link href="css/style.css" rel="stylesheet" title="normal" type="text/css"/>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<!--    <script src="styleswitcher.js" type="text/javascript"> </script> -->
    <title>-- zirael.org --</title>
  </head>

  <body>
    <h1>zirael.org</h1>
    <p>Welcome to zirael.org. If you are looking for BKchem you can find it <a href="http://bkchem.zirael.org/index.html">here</a>.</p>

    <p class="transparent">
      <a href="http://www.gnu.org/software/emacs/">
	<img alt="Created with Emacs" height="33" src="img/emacs.png" style="border:0" width="88" class="down"/>
      </a>
      <a href="http://validator.w3.org/check/referer">
	<img alt="Valid XHTML 1.0!" height="31" src="http://www.w3.org/Icons/valid-xhtml10" style="border:0" width="88" class="down"/>
      </a>

    </p>

  </body>
</html>

Doba běhu: 26.1 ms