Python-2.7.5

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1.Python获取图片保存本地

#coding=utf-8

#re模块主要包含了正则表达式

import re

#Urllib模块提供了读取web页面数据的接口

import urllib

#getHtml()函数传递一个网址并把整个页面下载下来

def getHtml(url):

    page = urllib.urlopen(url)

    html = page.read()

    return html

#getImg()函数用于在获取的整个页面中筛选需要的图片连接并保存本地

def getImg(html):

    reg = r'src="(.+?\.jpg)" pic_ext'

    imgre = re.compile(reg)

    imglist = re.findall(imgre,html)

    x = 0

    for imgurl in imglist:

        urllib.urlretrieve(imgurl,'%s.jpg' % x)

        x+=1

#传递的网页地址并打印输出

html = getHtml("http://tieba.baidu.com/p/2460150866")

print getImg(html)


2.Python操作mysql数据库

#coding=utf-8

import MySQLdb

#连接mysql功能

conn= MySQLdb.connect(

        host='localhost',

        port = 3306,

        user='root',

        passwd='123456',

        db ='test',

        )

cur = conn.cursor()

#创建数据表

#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一条数据

#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

#修改查询条件的数据

#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#删除查询条件的数据

#cur.execute("delete from student where age='9'")

#关闭mysql连接

cur.close()

conn.commit()

conn.close()

2.1报错ImportError: No module named MySQLdb

该错误是源于我们没有安装Python连接MySQL所需的MySQLdb库而引起

Linux Fedora, CentOS系统:yum install MySQL-python

Linux Ubuntu操作系统:apt-get install python-mysqldb


3.Python设置测试万能验证码

#coding=utf-8

import random

#生成0到10之间的随机数

#d = random.uniform(0,10)

#print d

#生成一个1000到9999之间的随机整数

d = random.randint(1000,9999)

print u"生成的随机数:%d " %d

i = input(u"请输入随机数:")

print i

if i == d:

    print u"登录成功!!"

elif i == 1111:

    print u"登录成功!!"

else:

    print u"请重新输入验证码!"


4.Python扫描指定IP开放端口号

# -*- coding:utf8 -*-

#!/usr/bin/python


# Python:          2.7.5

# Platform:        linux

# Authro:          wc

# Program:         port scan

# History:         2015.6.1


import socket, time, thread

socket.setdefaulttimeout(3)


def socket_port(ip,port):

    """

    echo IP and port , judge port opening or closing

    """

    try:

        if port>=65535:

            print u'scan port end'

        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        result=s.connect_ex((ip,port))

        if result==0:

            lock.acquire()

            print  ip,u':',port,u'port opening'

            lock.release()

        s.close()

    except:

        print u'port error'


def ip_scan(ip):

    """

    echo IP , scan 0-65534 port

    """

    try:

        print u'start scan %s' % ip

        start_time=time.time()

        for i in range(0,65534):

            thread.start_new_thread(socket_port,(ip,int(i)))

        print u'scan end , total time : %.2f' %(time.time()-start_time)

        raw_input("Press Enter to Exit")

    except:

        print u'scan ip error'


if __name__=='__main__':

    url=raw_input('Input the ip you want to scan:\n')

    lock=thread.allocate_lock()

    ip_scan(url)


5.python实现简单时钟输出

import sys, signal, time

def now(): return time.ctime(time.time( ))


def onSignal(signum, stackframe):

    print 'Got alarm', signum, 'at', now( )


while 1:

    print 'Setting at', now( )

    signal.signal(signal.SIGALRM, onSignal)

    signal.alarm(5)

    signal.pause( )


6.python-2.7.5集成Django-1.9


7.报错1:ImportError: No module named M2Crypto

解决1:yum install m2crypto



本文转自 guowang327 51CTO博客,原文链接:http://blog.51cto.com/guowang327/1723489,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8月前
|
索引 Python
快来看啊!原来Python里还有这些的一些有趣的东西!
快来看啊!原来Python里还有这些的一些有趣的东西!
34 0
|
10月前
|
设计模式 自然语言处理 JavaScript
【21天python打卡】第1天 python预备知识(1)
大家好,今天是21天python打卡的第一天,我们要想学好python,我们先了解一些关于python的基础知识。
101 0
|
11月前
|
Python
Python生日蛋糕
Hello,小伙伴们晚上好吖!前两天博主满20岁啦(要开始奔三辽呜呜呜),这几天收到了不少小伙伴们的祝福,浪漫的小博主想送给大家一份不一样的生日蛋糕,感谢大家对俺滴支持!
106 0
|
12月前
|
Python
【Python三体问题】
【Python三体问题】
109 0
【Python三体问题】
|
IDE 开发工具 Python
万事开头难——正确开始使用Python
万事开头难——正确开始使用Python
59 0
万事开头难——正确开始使用Python
|
Python
Python:使用2to3将Python2转Python3
Python:使用2to3将Python2转Python3
73 0
|
数据处理 Python
python 小知识 - 类全知道
类是面向对象编程的基础,是python开发过程中需要重要掌握的知识点。
138 0
python 小知识 - 类全知道
|
数据采集 运维 算法
|
缓存 算法 Python
用 Python 把坤坤动起来
最近看到一个 Up 主 Ele实验室 发布的一个视频:字符化视频是怎么做出来的,感觉很有意思。不如自己也实现一个来玩玩? 以前也没怎么写过 Python,只用来刷过 LeetCode。正好借这个机会再学一学 Python 吧。
用 Python 把坤坤动起来