栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

软件工程项目实训08

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

软件工程项目实训08

一,本周工作 1,学习python连接数据库
import pymysql
from ip_port_get import *
def insert(conn, cursor, ip, port):
    sql = "insert into unite_server values (0,'%s','%d')"% (ip,port)
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Commit your changes in the database
        conn.commit()
        print(sql)
        print('Yes, Insert Successfull.')
    except:
        # Rollback in case there is any error
        conn.rollback()
    # disconnect from server
def find(conn, cursor):
    # Prepare SQL query to select a record from the table.
    # sql = "SELECt * FROM unite_server 
    #        WHERe ip = '%s'and port = '%s'" % (ip, port)
    sql = "select * from unite_server"
    # print (sql)
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        return results
    except:
        import traceback
        traceback.print_exc()
        print("Error: unable to fetch data")
        return None
def find_exist(conn, cursor, ip, port):
    sql = "select * from unite_server where ip='%s' and port = '%d'" % (ip, port)
    # print (sql)
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        if len(results) == 0:
            print("not exist")
            return False
        else :
            print('exist')
            return True
    except:
        import traceback
        traceback.print_exc()
        print("Error: unable to fetch data")
        return False
def delete(conn, cursor, ip, port):
    sql = "DELETE FROM unite_server WHERe ip='%s' and port='%d'" % (ip,port)
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Commit your changes in the database
        conn.commit()
        print("delete success")
    except:
        # Rollback in case there is any error
        conn.rollback()
        print("delete fail")
def update(conn, cursor, ip, port):
    # Prepare SQL query to UPDATE required records
    temp = port + 10
    sql = "UPDATE unite_server SET port = '%d' 
                              WHERe ip = '%s' and port = '%d'" % (temp,ip,port)
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Commit your changes in the database
        conn.commit()
        print('update success')
    except:
        # Rollback in case there is any error
        conn.rollback()
        print('update failure')
if __name__ == '__main__':
    conn = pymysql.connect(host='rm-bp1ocx5t725vc9t85fo.mysql.rds.aliyuncs.com', port=3306, user='user1', db='test1',
                           password='chy352196487!')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    results = find(conn, cursor)
    update(conn,cursor, '10.27.251.212', 9011)
    conn.close()


2,学习获得自己的ip和port

# 目标:获取当前的Ip和空闲port
import os
import random
import socket

def get_ip():
    """
    查询本机ip地址
    :return: ip  10.27.251.212
    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))  # 本质是用当前的去建立一个连接8.8.8.8,port=80的网页的socket,就可以得到自己的了
        ip = s.getsockname()[0]
    finally:
        s.close()

    return ip
def get_port():
    # pscmd = "netstat -ntl |grep -v Active| grep -v Proto|awk '{print $4}'|awk -F: '{print $NF}'"
    pscmd = "netstat -nt |findstr -v Active| findstr -v Proto"
    procs = os.popen(pscmd).read()
    procarr = procs.split("n")
    tt= random.randint(9000, 9100)
    if tt not in procarr:
        return tt
    else:
        return get_port()
if __name__ == '__main__':
    print(get_ip())
    print(get_port())
2,遇到的问题

期间在环境配置上除了很多问题,但都通过csdn解决了,同时为了更好的进行测试,学习了postman发送http请求:

 

二,下周安排

支援后端,完成登录注册功能

解决遗留问题,使用socket与他人通过网线传输,接收数据很缓慢,同时不时还会出现数据丢失,总之就是不可靠

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947551.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号