HTTP-Klasse
Diese Klasse stellt einige sehr, sehr einfache Methoden für den Umgang mit GET- und POST-Requests bereit. Sie bietet weder Cookie-Management noch die automatische Behandlung von Weiterleitungen (HTTP 30_).
# -*- coding:iso-8859-15 -*-
import urllib # urlencode
import httplib # HTTPConnection
import random # randint
USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12"
ERROR_MESSAGES = {
100:"Continue",
101:"Switching protocols",
200:"OK",
201:"Created",
202:"Accepted",
203:"Non-authoritative information",
204:"No content",
206:"Partial content",
300:"Multiple Choices",
301:"Moved permanently",
302:"Found",
303:"See other",
304:"Not modified",
305:"Use proxy",
307:"Moved temporarily",
400:"Bad request",
401:"Unauthorized",
402:"Payment required",
403:"Forbidden",
404:"Not found",
405:"Method not allowed",
406:"Not acceptable",
407:"Proxy authentification required",
408:"Request time-out",
409:"Conflict",
410:"Gone",
411:"Length required",
412:"Precondition failed",
413:"Request Entity Too Large",
414:"Request URL too long",
415:"Unsupported media type",
416:"Request range not satisfiable",
417:"Expectation failed",
424:"Site too ugly",
500:"Internal Server Error",
501:"Not implemented",
502:"Bad gateway",
503:"Service unavailible",
504:"Gateway time-out",
505:"HTTP version not supported"
}
class Error(Exception):
def __init__(self,code,content):
self.code = code
self.message = ERROR_MESSAGES[code]
self.content = content
class HTTP:
""" Eine einfache HTTP-Klasse """
def __init__(self,host,ua,https=False):
self.host = host
self.ua = ua
self.headers = {}
self.secure = https
self.status = 0
def get(self,path="/",data={},headers={}):
if self.secure:
conn = httplib.HTTPSConnection(self.host)
else:
conn = httplib.HTTPConnection(self.host)
data = urllib.urlencode(data)
headers["User-Agent"] = self.ua
headers["Connection"] = "close"
conn.request("GET",path+"?"+data,{},headers)
response = conn.getresponse()
conn.close()
self.headers = {}
if response.status >= 400:
raise Error,(response.status,response.read())
self.content = response.read()
self.headers = response.getheaders()
self.status = response.status
return True
def post(self,path,getdata={},postdata={},headers={}):
if self.secure:
conn = httplib.HTTPSConnection(self.host)
else:
conn = httplib.HTTPConnection(self.host)
getdata = urllib.urlencode(getdata)
postdata = urllib.urlencode(postdata)
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["User-Agent"] = self.ua
headers["Connection"] = "close"
conn.request("POST",path+"?"+getdata,postdata,headers)
response = conn.getresponse()
conn.close()
self.headers = {}
if response.status >= 400:
raise Error,(response.status,response.read())
self.content = response.read()
self.headers = response.getheaders()
self.status = response.status
return True
def getContent(self):
return self.content
def getHeader(self,name):
for header in self.headers:
if header[0].lower() == name.lower():
return header[1]
return ""
Snippetdetails
- hinzugefügt: 22.11.2008
- aktualisiert: 22.11.2008
- Snippet herunterladen
Kommentar verfassen
Fehler gefunden? Doofer Code? Ein kleines "Danke!"? Hinterlasse einfach einen Kommentar.
Dein Kommentar wird erst nach einer manuellen Prüfung angezeigt.