---
title: "python 学习"
author: "ileson"
date: "2015-02-12T22:00:07Z"
tags: []
categories: ["编程语言"]
slug: "python"
draft: false
lastmod: "2015-03-08T14:42:56Z"
id: 2185508
url: "https://ileson.iteye.com/blog/2185508"
---

学习了一门胶水型 的语言：python   服务器脚本语言。用起来还不错。

官方网站:[https://www.python.org/](https://www.python.org/)

一、语言基础
   1、安装

   2、入门
 

   3、将脚本，用py2exe 打包成windows 下的exe 文件。在其它没有安装python 环境的电脑也能运行你写的脚本。
    setup.py 内容：
    
```python

#-*-coding: UTF-8-*-
from distutils.core import setup
import py2exe
setup(console=["tcpserver.py"])

```


    TCPServer.py 内容：


```python
import socket, traceback
import os
from time import ctime

host = ''                               # Bind to all interfaces
port = 20000
BUFSIZE = 1024

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.bind((host, port))
#这里设定每次最多只有一个等候处理的请求，真正的服务器会设置一个很高的数字。
serverSocket.listen(5)

os.system("title TCP Server") #设置控制台的标题。

print("-----------------------------------------------------")
print("服务已经启动！ ^_^ Please wait client connection！")
print("          Mode  ： TCP ")
print("Listen Address  ： 127.0.0.1:20000 ")
print("         Autor  ： 278952725  leson")
print("       Version  ： 1.0")
print("-----------------------------------------------------")

while 1:
    try:
        clientSocket, clientaddr = serverSocket.accept()
    except KeyboardInterrupt:
        raise
    except:
        traceback.print_exc()
        continue

    # Process the connection
    try:
        # Process the request here
        # while 1:
            data = clientSocket.recv(BUFSIZE).decode()
            print ("Got connection from", clientSocket.getpeername(),data)
            #os.system(data);
            #clientSocket.send(('[%s] %s' % (ctime(), data)).encode())  
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()

    # Close the connection
    try:
        clientSocket.close()
    except KeyboardInterrupt:
        raise
    except:
        traceback.print_exc()
```

    执行如下命令即可打包成一个发布文件夹 dist ,将这个文件夹拷贝到其它机器上就可以跑了。
    
```python
python setup.py py2exe
```

    注意：要将python 的安装目录配置到环境变量里去，否则将报找不到python 命令。

    [打包相关](http://sucre.iteye.com/blog/711344)


二、问题
  1、注意：2.x 版本和 3.x版本不兼容；



学习：[视频教程](http://www.imooc.com/learn/177)
[]w3cschool   python3.x入门教程](http://www.w3cschool.cc/python3/python3-tutorial.html)
[w3cschool入门教程](http://www.w3cschool.cc/python/python-tutorial.html)