Python implementation setting Windows desktop wallpaper code sharing _python

Source: Internet
Author: User
Tags auth base64 urlencode

Change a wallpaper every day, a good mood every day.

#-*-Coding:utf-8-*-from __future__ import unicode_literals import Image import datetime import Win32gui,win32con,w
  IN32API import re from httpwrapper import sendrequest storefolder = "C:\\dayimage" def setwallpaperfrombmp (ImagePath): K = Win32API. RegOpenKeyEx (Win32con. HKEY_CURRENT_USER, "Control Panel\\Desktop", 0,win32con. Key_set_value) Win32API. RegSetValueEx (k, "Wallpaperstyle", 0, Win32con. REG_SZ, "2") #2拉伸适应桌面, 0 desktop centered Win32API. RegSetValueEx (k, "Tilewallpaper", 0, Win32con. REG_SZ, "0") Win32gui. SystemParametersInfo (Win32con.  Spi_setdeskwallpaper,imagepath, 1+2) def setwallpaper (ImagePath): "" "Given a path to a image, convert it to BMP and Set it as Wallpaper "" "Bmpimage = Image.open (imagepath) NewPath = Storefolder + ' \\mywallpaper.bmp ' Bmpimage.sav E (NewPath, "BMP") Setwallpaperfrombmp (NewPath) def getpicture (): url = "Http://photography.nationalgeographic.com/pho tography/photo-of-the-day/"h = sendrequest (URL) if H.getsource (): R = Re.findalL (' <div class= "Download_link" ><a href= "(. *?)" >download ', H.getsource ()) if R:return SendRequest (r[0)). GetSource () else:print "Resolve the picture address error, check that the regular expression is correct" return to None def setwallpaperoftoday (): img = Getpictur E () If Img:path = Storefolder + "\\%s.jpg"% datetime.date.today () F = open (path, "WB") F.write (IMG) F.C

 Lose () setwallpaper (path) setwallpaperoftoday () print ' wallpaper set ok! '

The Httpwrapper in


is the encapsulation of an HTTP access I wrote:

#!/usr/bin/env python #-*-coding:utf-8-*-#---------------------------------------------------------------------- ----------------------------------Import Base64 Import urllib import urllib2 import time import re import sys class Sen Drequest: "" "Page Request enhanced class SendRequest (' http://xxx.com ', data=dict, type= ' POST ', auth= ' base ', user= ' xxx ', password= ' xxx ')" "
  "Def __init__ (self, URL, data=none, method= ' get ', Auth=none, User=none, password=none, cookie = None, **header):" " URL: Requested URL, cannot be null date: The content that requires post, must be a dictionary method:get or post, default to get auth: ' base ' or ' cookie ' User: Username for base authentication p
  Assword: A password cookie for base authentication: The cookie that comes with the request, typically used for authentication other headers after login: e.g. referer= ' www.sina.com.cn ' "" "Self.url = URL Self.data = Data Self.method = Method Self.auth = Auth Self.user = user Self.password = password Self.cookie = c Ookie if ' referer ' in HeaDer:self.referer = Header[referer] Else:self.referer = None if ' user-agent ' in header:self.user_agent = Header[user-agent] Else: # # self.user_agent = ' mozilla/5.0 (Windows NT 5.1; rv:8.0) gecko/20100101 firefox/8.0 ' SE Lf.user_agent = ' mozilla/5.0 (iPhone; U CPU iPhone os 3_0 like Mac os X; En-US) applewebkit/528.18 (khtml, like Gecko) version/4.0 mobile/7a341 safari/528.16 ' self.__setuprequest () self.__Se

  Ndrequest () def __setuprequest (self): If Self.url is None or self.url = = ': Raise ' URL cannot be empty! ' #访问方式设置 if self.method.lower () = = ' Post ': self. Req = Urllib2. Request (Self.url, Urllib.urlencode (self.data)) elif self.method.lower () = = ' Get ': if Self.data = none:self. Req = Urllib2. Request (Self.url) else:self. Req = Urllib2. Request (Self.url + '? ' + Urllib.urlencode (self.data)) #设置认证信息 if Self.auth = = ' base ': if Self.user = None or sel
    F.password = = None:raise ' The user or password is not given! ' ELse:auth_info = base64.encodestring (Self.user + ': ' + self.password). replace (' \ n ', ') Auth_info = ' Basic ' + Auth_info self. Req.add_header ("Authorization", auth_info) elif Self.auth = ' Cookie ': if Self.cookie = = None:raise ' The COO
    Kie is not given! ' Else:self. Req.add_header ("Cookie", Self.cookie) if self.referer:self. Req.add_header (' Referer ', self.referer) if self.user_agent:self. Req.add_header (' user-agent ', self.user_agent) def __sendrequest (self): try:self. Res = Urllib2.urlopen (self. REQ) Self.source = self. Res.read () Self.code = self. Res.getcode () self.head_dict = self. Res.info (). Dict self. Res.close () except:print "Error:httpwrapper=>_sendrequest", Sys.exc_info () [1] def getresponsecode (self): " "" To get the status code returned by the server (200 for Success, 404 page does not exist) "" "" Return Self.code def GetSource (self): "" "" "to get the page source code, need to decode after using" "" "If" so Urce "in Dir (self): return self.source return u ' def getheaderinFo (self): "" U ' Get response header info ' "" Return self.head_dict def GetCookie (self): "" "" "to get the cookie returned by the server, generally used for login follow-up" "" If ' Set-cookie ' in Self.head_dict:return self.head_dict[' Set-cookie '] Else:return None def getcontenttype (SE n None def getcharset (self): "" "to try to get the encoding of a Web page if it does not return none" "" ContentType = self. getContentType () If contentType is not None:index = Contenttype.find ("charset") if index > 0:return c
   Ontenttype[index+8:] Return to None def getexpirestime (self): "" "" "" "" "" If ' Expires ' in Self.head_dict: "" "  Return self.head_dict[' expires '] Else:return None def getservername (self): "" "" Get Server Name "" If ' server ' in Self.head_dict:return self.head_dict[' server '] else:return None __all__ = [SendRequest,] if __name__ = ' __ma In__ ': b = sendrequest ("http://www.baidu.com") print B.getsource ()
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.