Saturday, May 16, 2015

MySQL Python 教程

Ubuntu
$ apt-cache search MySQLdb
$ sudo apt-get install python-mysqldb

打印版本号

#!/usr/bin/python
# -*- coding: utf-8 -*-

import _mysql
import sys

con = None

try:

    con = _mysql.connect('localhost', 'testuser', 
        'test623', 'testdb')
        
    con.query("SELECT VERSION()")
    result = con.use_result()
    
    print "MySQL version: %s" % \
        result.fetch_row()[0]
    
except _mysql.Error, e:
  
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)

finally:
    
    if con:
        con.close()

创建表,插入数据


#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb as mdb
import sys

con = mdb.connect('localhost', 'testuser', 'test623', 'testdb');

with con:
    
    cur = con.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS \
        Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))")
    cur.execute("INSERT INTO Writers(Name) VALUES('Jack London')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Emile Zola')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Truman Capote')")

Windows 下安装 Ubuntu 双系统

  1. 准备磁盘空间
    在磁盘管理中将要分配给 Ubuntu 的卷删除。
  2. 设置 iso 文件
    • 将镜像下的 casper 文件夹下的 vmlinuz 和 initrd.lz 解压出来,并复制在 C 盘根目录下;
    • 将准备好的 iso 镜像文件拷贝到某盘根目录下(如D盘);
  3. 安装 EasyBCD 软件
  4. 设置向导
    • 打开 EasyBCD 软件,选择 Add Entries,选 NeoGrub,然后点 Install,接着是Configure
    • 然后就会弹出 menu.lst 文件,我们要编辑这个文件,因为系统就是这个文件找到我们的 Ubuntu 的;
    • 把下面的英文复制到 menu.lst 文件末尾
         title Install Ubuntu
         root (hd0,0)
         kernel (hd0,0)/vmlinuz boot=casper iso-scan/filename=/ubuntu-11.10-i386.iso ro quiet splash locale=zh_CN.UTF-8
         initrd (hd0,0)/initrd.lz
      
      特别注意: ubuntu-11.10-i386.iso 是 Ubuntu 系统 iso 镜像文件的名字,需要根据所用版本进行更改; 对于有的电脑上你的第一个盘符并不是C盘,在磁盘管理中可以看出,所以安装时需将 (hd0,0) 改为 (hd0,1)【假设为第二个】; 64 位 Ubuntu 解压出来的文件为 vmlinuz.eti,因此须将上述代码中的 vmlinuz 改为vmlinuz.eti
  5. 重启
    重启,你就会看到有2个启动菜单给你选择,我们选择第2个 Grub 这个,选择install,然后等待一段时间,就会进入 Ubuntu 桌面了。
  6. 卸载镜像
    进入桌面之后,先在终端输入:
      $ sudo umount -l /isodevice
    
    这一命令取消掉对光盘所在驱动器的挂载,否则分区界面找不到分区。
  7. 安装
    一路 next 之后在安装类型界面,我们用自定义,即可以自定义分区的安装方法。
    选择在 Win7/8 中删除的空闲空间(以 40G 左右为例),点击创建,点击添加:
    具体分区如下(仅作参考): 分配 20G 给根目录,即 / ext4 分配 18G 给 /usr ext4 分配 120M 给 /boot ext4 分配 8G 给 /home ext4 分配 2G(所有剩下的,约为 2G)给交换空间 swep
    关于安装启动引导器的设备,会影响 Window 和 Ubuntu 的引导关系,此处默认即可。
  8. 重启
    注意:若重启发现原来 Windows 进不去了,打开终端输入命令:
      $ sudo gedit /etc/default/grub
    
    修改 GRUB_TIMEOUT="10",然后在终端中输入:
      $ sudo update-grub update
    
    命令会自动找到 Windows 启动项,并且自动更新 /boot/grub/grub.cfg 文件,这样重启就能进 Windows 了。
  9. 更改启动顺序将 Windows7/8 设为默认启动项
    若重启,发现 Windows 不是第一个启动项,则
      $ sudo mv /etc/grub.d/30_os-prober /etc/grub.d/06_os-prober
    
    即将 /etc/grub.d/30_os-prober 改名为 06_os-prober,再执行: $ sudo update-grub 即可。
  10. 进入 windows 进行善后工作
    最后进入 Window7/8,打开 EasyBCD 删除安装时改的 menu.lst 文件,按 Remove 即可。
    然后去我们的 C 盘删除 vmlinuzinitrd.lz 和系统的 iso 文件。
  11. Let's begin Linux。

python 做网络爬虫下载数据

#!usr/bin/python

import re
import urllib

def getHtml(url):
    page=urllib.urlopen(url)
    html=page.read()
    return html

def getImg(html):
    reg=r'(http://cache.+)" alt'
    imgre=re.compile(reg)
    imglist=re.findall(imgre,html)
    x=10
    for imgurl in imglist:
        trimurl=imgurl.replace("amp;","")
        urllib.urlretrieve(trimurl, '%s.jpg' % x)
        x=x+1
        print trimurl
   

html=getHtml("http://www.photos.com/prints/photographers/the-complete-slim-aarons-collection")

getImg(html)

Friday, May 15, 2015

ubuntu下创建eclipse桌面快捷方式

1.桌面目录创建一个名为“eclipse.desktop”的文件
并添加以下内容:

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse Platfrom
Comment=Eclipse IDE
Exec=/home/jason/eclipse/eclipse
Icon=/home/jason/eclipse/icon.xpm
Terminal=false
StartupNotify=true
Type=Application
Categories=Application;Development;



注意:
1 Exec=/home/jason/eclipse/eclipse  
2 Icon=/home/jason/eclipse/icon.xpm  
这个地方要修改为你的安装目录。
2.然后给该文件赋权 ,运行“chmod u+x eclipse.desktop“命令即可。
3.直接cp上面代码的朋友注意了,cp回去编辑的时候注意去掉每行后面的空格。

Wednesday, May 13, 2015

Java Notes

The output of Java compiler is not executable code but byte-code to be executed by JVM.
multitasking: process-based and thread-based
for java multi-thread programming

Friday, May 1, 2015

nginx负载平衡

upstream site {
server localhost:85;      //apache服务器
server localhost:86;
}

    server {
       listen         80;
       server_name    localhost;
       return         301 https://$server_name$request_uri;   //重写路径
}



server {
       
      listen 443 ssl;                                   //监听443端口 ,所有80转向到443
    server_name localhost;

    ssl on;
    ssl_certificate         server.crt;                  //ssl配置文件
    ssl_certificate_key    server.key;
   # ssl_trusted_certificate /etc/nginx/ssl/example.com/ca-certs.pem;

    location / {
        proxy_pass http://site;                                   //注意不用https
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
}

利用htmlunit和webdriver读取网页数据

先说.net的,需要进入 using mshtml;

           System.Net.WebClient webClient = new System.Net.WebClient();
              IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://course.xxx.edu/");
            String pageSource = driver.PageSource;
这种比较笨重,需要启动firefox读取


对java的,需要使用htmlunit包
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_31);
webClient.getOptions().setJavaScriptEnabled(true);
//webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(false);
   webClient.getOptions().setCssEnabled(false);

   HtmlPage page =
webClient.getPage("http://course.xxx.edu");
 
webClient.waitForBackgroundJavaScript(5000);


String text=page.asText();
/*String patternString = "Available Seats:";
Pattern pattern = Pattern.compile(patternString);

        Matcher matcher = pattern.matcher(text);*/
        int index=text.lastIndexOf("Available Seats");
        String seats=text.substring(index+17, index+19).trim();

int numOfSeats=Integer.parseInt(seats);
System.out.println(numOfSeats);


如何利用c#发送gmail
public static void SendMail(String s)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("sender@gmail.com");
            mail.To.Add("receiver@gmail.com");
           // mail.To.Add("m2@gmail.com");
            mail.Subject = "good news: "+s;
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";
            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";
            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";
            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "mypassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

        }