#!/usr/bin/env python #encoding: utf-8 #qq:831937 import os import base64 #输入文件名,打印出每行的base64编码 filename = raw_input('Enter file name:') if os.path.isfile(filename): fobj = open(filename,'r+') #文件对象访问模式:读写 for eachLine in fobj: print base64.encodestring(eachLine), #print默认会在行尾加换行符,用逗号避免这个行为 fobj.close() #关闭文件 else: print 'file is not exist' import random rndnum = random.randint(0,9) #随机生成0-9的数字 print rndnum import urllib2 content = urllib2.urlopen('http://localhost').read() #抓取页面内容 print content #http请求 headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0', 'Referer':'http://localhost/', 'X-Forwarded-For': '8.8.8.8' } import urllib,urllib2,cookielib postdata=urllib.urlencode( { 'username':"user", 'password':'pwd', } ) req = urllib2.Request( url = 'http://localhost/', headers = headers, data = postdata, ) result = urllib2.urlopen(req).read() #http请求支持响应cookie import urllib2,cookielib cookie=cookielib.CookieJar() cookiehandler=urllib2.HTTPCookieProcessor(cookie) opener=urllib2.build_opener(cookiehandler) content = urllib2.urlopen('http://localhost/').read()