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与他人通过网线传输,接收数据很缓慢,同时不时还会出现数据丢失,总之就是不可靠



