关于Python一直只是听别人说,但是没有机会去真正学习它。
最近正好有一些空闲时间,然后就抓住机会比较全面的学习了一下。
由于自己做的是APP客户端,经常与服务端有接触,所以就再把Python与服务端相关的进行了实践,并把实践进行了总结
我学习Python的基础教程网站
打开Python官网下载页面,选择Python2、Python3进行下载,下载完成后进行安装就可以了
打开PyCharm官网下载页面,选择相应环境进行下载
安装完成后,在***File—>Settings—>Project***设置Python.exe路径
打开MySQL官网下载页面,选择32位下载安装
##2.4.安装MySql Connector
Python连接数据库需要Connector,可以从MySQL Connector官网下载,选择32位下载安装
#3.基本语法
关于Python基本语法,主要学习了***变量类型、条件判断、循环、列表、元组、字典、时间、函数***等
学习完成后,则可以实现下图的一个小程序中的加法逻辑处理
#!/usr/bin/env python#coding=utf-8import randomimport sysimport signal total_count = 0invalid_count = 0right_count = 0error_count = 0def sigint_handler(signum,frame): global is_sigint_up is_sigint_up = True print "\nByeBye\n" sys.exit()signal.signal(signal.SIGINT,sigint_handler)signal.signal(signal.SIGHUP,sigint_handler)signal.signal(signal.SIGTERM,sigint_handler)is_sigint_up = Falsewhile True: rand1 = random.randint(1,9) rand2 = random.randint(1,9) var1 = raw_input("input "+bytes(rand1)+"+"+bytes(rand2)+" value: ") #print "you input integer var1 is: " + var1 while var1 == 'r' or var1.isdigit() == False or var1 == 'q': if var1 == 'r': total_count = right_count + error_count; accuracy_value = 0 if total_count > 0: accuracy_value = float(right_count)/float(total_count) * 100 print "\n" print "\033[4;37mtotal : \033[1;36m" + bytes(total_count) + "\033[0m\033[0m" print "invalid : \033[1;33m" + bytes(invalid_count) + "\033[0m" print "right : \033[1;32m" + bytes(right_count) + "\033[0m" print "error : \033[1;31m" + bytes(error_count) + "\033[0m" print "\033[1;46maccuracy: %.2f" % (accuracy_value) + "%" + "\033[0m" print "\n" var1 = raw_input("input "+bytes(rand1)+"+"+bytes(rand2)+" value: ") if var1 == 'q': sys.exit() if var1.isdigit() == False: print "your value is not digit \n" invalid_count += 1 var1 = raw_input("input "+bytes(rand1)+"+"+bytes(rand2)+" value: ") count_value = rand1 + rand2 print "the right value is : " + bytes(count_value) if count_value == int(var1): print "your result is right \n" right_count += 1 else: print "your result is error \n" error_count += 1 if is_sigint_up == True: print "is sigint up True"
学习了Python相应的包、类内容后,则可以实现上面小程序的完整功能了,其对应github代码
由于MySQL最新版本使用密码强加密方式,通过MySQLdb来连接登录不能成功,所以选择了***mysql.connector***来登录MySQL
相应的使用方式,可以参考MySQL的Demo
我的MySQL Connector Demo
def __create_database(self): try: cursor.execute("CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(CalculateDB.DATABASE_NAME)) cnx.database = CalculateDB.DATABASE_NAME self.mPrintUtil.show("Create database finish") except mysql.connector.Error as err: self.mPrintUtil.show("Failed creating database: {}".format(err)) exit(1) def __create_table(self): for name, ddl in CalculateDB.TABLES.iteritems(): try: self.mPrintUtil.show("Creating table {}: ".format(name),) cursor.execute(ddl) except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: self.mPrintUtil.show("already exists.") else: self.mPrintUtil.show(err.msg) exit(1) else: self.mPrintUtil.show("OK") def insert_to_calculate(self, mCalculateData): data_calculate = (mCalculateData.calculate_arg1, mCalculateData.calculate_arg2, mCalculateData.calculate_operator_arg, mCalculateData.calculate_operator_str, mCalculateData.calculate_result, mCalculateData.input_calculate_result, mCalculateData.is_input_calculate_result_right, mCalculateData.calculate_date) cursor.execute(CalculateDB.INSERT_CALCULATE_SQL, data_calculate) cnx.commit() def query_to_calculate_total_count(self): cursor.execute(CalculateDB.QUERY_CALCULATE_TOTAL_COUNT_SQL) for(total_count, ) in cursor: return total_count def close_db(self): cursor.close() cnx.close()
用户通过CGI可以访问服务器中部署的Python脚本,我使用的是Tomcat,参考Python CGI初体验
>注意: >1.参考文章中"print('Content-type: text/html')",其在type:后面多了一个空格,所以网页访问服务器的Python脚本出错,页面没有内容显示 >2.CGI的servlet中要添加``` executable C:/Python34/python.exe ``` >
我的[CGI 链接](http://118.25.178.69/cgi_server/cgi/cgi_query_calculate.py "CGI 链接") >我的[CGI Demo](https://github.com/zxlworking/calculate/blob/master/cgi_query_calculate.py "CGI Demo") >![这里写图片描述](https://www.www.zyiz.net/i/?i=20180612161018754?)
在web服务中信息传输常用JSON格式进行传输,所以也要学习使用Python的JSON处理
我的JSON 链接
我的JSON Demo