Last Tested in Python Version: 3.1.2
Basically an HTTP Wrapper for Python.
Feel free to add any functions you'd like to see in the class, or make any changes. I'll gladly edit the post here.
If anyone can manage to add Proxy support to this class, it would be beneficial for everyone.
Again, it hasn't been fully documented yet. I'll try to get to it ASAP.
Changelog:Code:#!/usr/bin/env python ##################### #Author: Riddle #Website: www.logicalgamers.com #E-mail: NONE #Version: 0.9.1 ##################### import general from urllib.parse import urlencode import urllib.request import gzip import http.cookiejar import socket import io socket.setdefaulttimeout(10) class HTTP: def __init__(self, user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.86 Safari/533.4'): self.USER_AGENT = user_agent self.ENCODING = 'gzip' self.ID = None self.Cookie_Jar = http.cookiejar.LWPCookieJar() self.Last_URL = None self.Referer = None def Set_Referer (self, Ref = None): self.Referer = Ref def Clear_Cookies (self): self.Cookie_Jar.clear_session_cookies() def Get_Opener (self): Opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.Cookie_Jar)) if self.Referer != None and self.ENCODING != None: Opener.addheaders = [('User-Agent', self.USER_AGENT),('Accept-Encoding', self.ENCODING),('Referer', self.Referer)] elif self.Referer != None and self.ENCODING == None: Opener.addheaders = [('User-Agent', self.USER_AGENT),('Referer', self.Referer)] elif self.Referer == None and self.ENCODING != None: Opener.addheaders = [('User-Agent', self.USER_AGENT), ('Accept-Encoding', self.ENCODING)] else: Opener.addheaders = [('User-Agent', self.USER_AGENT)] self.Referer = None return Opener def POST (self, URL, Data): if Data.__class__ == dict: Data = urllib.parse.urlencode(Data) return self.GET(URL, Data) def GET (self, URL, Data = None, enc = "utf-8"): try: Request = urllib.request.Request(URL, Data) Opener = self.Get_Opener() Contents = Opener.open(Request) Source = Contents.read() if self.ENCODING == 'gzip': try: Compressed_Data = io.BytesIO(Source) Decompressed_Data = gzip.GzipFile(fileobj = Compressed_Data) Source = Decompressed_Data.read() except: general.General().Log_Error("Error decoding GZip.\nURL: " + URL + "\nData: " + Data + "\n\n") return str(Source,enc) except: return "__ERROR__" def Get_Last_URL(self): return self.Last_URL def Set_Last_URL(self, URL = None): self.Last_URL = URL def Save_Image(self, URL, path = None): Opener = urllib.request.build_opener() Contents = Opener.open(URL) Image = Contents.read() general.General().Str_To_File(Image,path,'wb')
- 6/30/2010: Added version doc. in the class. Need to add proxy support.
- 7/4/2010: Version 0.9.1; Added Save_Image().
Results 1 to 5 of 5
Thread: [Python] Session Class
- 30 Jun. 2010 12:37am #1
[Python] Session Class
Last edited by Riddle; 04 Jul. 2010 at 11:01pm.
- 01 Jul. 2010 01:50am #2
- 04 Jul. 2010 11:02pm #3
- 15 Apr. 2011 01:44am #4
It should probably be noted that the general class he is referring to is this class: http://forum.logicalgamers.com/progr...ral-class.html
- 21 Apr. 2011 02:09am #5
Updated for Python 3.2. Just an issue with encoding:
Code:#!/usr/bin/env python ##################### #Author: Riddle #Website: www.logicalgamers.com #E-mail: NONE #Version: 0.9.1 ##################### import general from urllib.parse import urlencode import urllib.request import gzip import http.cookiejar import socket import io socket.setdefaulttimeout(10) class HTTP: def __init__(self, user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.86 Safari/533.4'): self.USER_AGENT = user_agent self.ENCODING = 'gzip' self.ID = None self.Cookie_Jar = http.cookiejar.LWPCookieJar() self.Last_URL = None self.Referer = None def Set_Referer (self, Ref = None): self.Referer = Ref def Clear_Cookies (self): self.Cookie_Jar.clear_session_cookies() def Get_Opener (self): Opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.Cookie_Jar)) if self.Referer != None and self.ENCODING != None: Opener.addheaders = [('User-Agent', self.USER_AGENT),('Accept-Encoding', self.ENCODING),('Referer', self.Referer)] elif self.Referer != None and self.ENCODING == None: Opener.addheaders = [('User-Agent', self.USER_AGENT),('Referer', self.Referer)] elif self.Referer == None and self.ENCODING != None: Opener.addheaders = [('User-Agent', self.USER_AGENT), ('Accept-Encoding', self.ENCODING)] else: Opener.addheaders = [('User-Agent', self.USER_AGENT)] self.Referer = None return Opener def POST (self, URL, Data): if Data.__class__ == dict: Data = urllib.parse.urlencode(Data) Data = Data.encode( 'utf8' ) # Fix for version 3.2. Convert to bytes. return self.GET(URL, Data) def GET (self, URL, Data = None, enc = "utf-8"): try: Request = urllib.request.Request(URL, Data) Opener = self.Get_Opener() Contents = Opener.open(Request) Source = Contents.read() if self.ENCODING == 'gzip': try: Compressed_Data = io.BytesIO(Source) Decompressed_Data = gzip.GzipFile(fileobj = Compressed_Data) Source = Decompressed_Data.read() except: general.General().Log_Error("Error decoding GZip.\nURL: " + URL + "\nData: " + Data + "\n\n") return str(Source,enc) except: return "__ERROR__" def Get_Last_URL(self): return self.Last_URL def Set_Last_URL(self, URL = None): self.Last_URL = URL def Save_Image(self, URL, path = None): Opener = urllib.request.build_opener() Contents = Opener.open(URL) Image = Contents.read() general.General().Str_To_File(Image,path,'wb')