devops/script/tools.ipynb
2025-12-12 11:40:38 +08:00

707 lines
24 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60\n",
"59\n"
]
},
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: '22\\n04'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 18\u001b[0m\n\u001b[0;32m 16\u001b[0m l2 \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 17\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m a1:\n\u001b[1;32m---> 18\u001b[0m l1\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m:\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[0;32m 19\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m a2:\n\u001b[0;32m 20\u001b[0m l2\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28mint\u001b[39m(i))\n",
"\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '22\\n04'"
]
}
],
"source": [
"str = \"\"\"0@0@0@165@0:1807 0@1@0@167@0:1803 0@2@0@-1@0:1531 0\n",
"@3@0@155@0:1806 0@4@0@-1@0:1224 0@5@0@77@0:1207 0@6@0@-1@0:1222 0@7@0@-1@0:1523 0@8@0@-1@0:1521 1@0@0@147@0:1802 1@1@0@146@0:1801 1@2@0@205@0:2328 1@3@0@-1@0:2026 1@4@0@-1@0:1221 1@5@0@189@0:2302 1@6@0@187@0:22\n",
"04 1@7@0@-1@0:1511 1@8@0@137@0:1408 2@0@0@152@0:1805 2@1@0@206@0:1602 2@3@0@204@0:2506 2@5@0@-1@0:2422 2@6@0@201@0:2403 2@7@0@-1@0:1866 2@8@0@198@0:2305 3@0@0@86@0:1107 3@1@0@-1@0:1512 3@2@0@186@0:2301 3@3@0@-1\n",
"@0:1531 3@4@0@-1@0:1531 3@5@0@-1@0:2424 3@6@0@-1@0:1532 3@7@0@148@0:1804 3@8@0@168@0:2003 4@0@0@191@0:2007 4@1@0@-1@0:1535 4@3@0@185@0:2107 4@4@0@-1@0:2426 4@5@0@200@0:2401 4@6@0@199@0:2402 4@7@0@166@0:2002 4@8\n",
"@0@160@0:2001 5@0@0@139@0:1705 5@1@0@-1@0:2524 5@2@0@181@0:2202 5@3@0@177@0:2105 5@4@0@203@0:563 5@5@0@183@0:2203 5@6@0@182@0:2201 5@7@0@176@0:2101 5@8@0@112@0:1704 6@0@0@104@0:1701 6@1@0@130@0:1703 6@2@0@151@0\n",
":1901 6@3@0@159@0:1905 6@4@0@142@0:1706 6@5@0@154@0:1903 6@6@0@157@0:1904 6@7@0@158@0:1902 6@8@0@188@0:1906\"\"\"\n",
"\n",
"str2 = \"\"\"1107 1701 1207 1704 1703 1705 1706 1801 1802 1804 1901 1805 1806 1904 1902 1903 2001 2002 1803 2003 2101 1807 2105 2202 2201 2203 2107 2204 2301 1866 1905 1906 2302 2007 1408 2305 563 2506 25\n",
"24 2026 2328 1523 1511 1224 2401 1222 2402 2403 2424 2426 2422 1512 1221 1531 1535 1531 1521 1533 1522\"\"\"\n",
"\n",
"a1 = str.split(\" \")\n",
"a2 = str2.split(\" \")\n",
"print(len(a1))\n",
"print(len(a2))\n",
"l1 = []\n",
"l2 = []\n",
"for i in a1:\n",
" l1.append(int(i.split(\":\")[1]))\n",
"for i in a2:\n",
" l2.append(int(i))\n",
"l1.sort()\n",
"l2.sort()\n",
"print(l1)\n",
"print(l2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"user_name=13100024182, auto_id=89 已更新\n",
"user_name=13100024183, auto_id=90 已更新\n",
"user_name=13100024011, auto_id=106 已更新\n",
"user_name=13100024012, auto_id=107 已更新\n",
"user_name=13100024081, auto_id=108 已更新\n",
"user_name=13100024082, auto_id=109 已更新\n",
"user_name=13100024161, auto_id=110 已更新\n",
"user_name=13100024162, auto_id=111 已更新\n"
]
},
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
"\u001b[1;31m请查看单元格中的代码以确定故障的可能原因。\n",
"\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
"\u001b[1;31m有关更多详细信息请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
]
}
],
"source": [
"import pymysql\n",
"from sshtunnel import SSHTunnelForwarder\n",
"server = SSHTunnelForwarder(\n",
" ssh_address_or_host=(\"1.15.182.107\", 22),\n",
" ssh_username=\"root\",\n",
" ssh_password=\"`NS?VGg@7]~F3}p\",\n",
" remote_bind_address=(\"127.0.0.1\",3306),\n",
" local_bind_address=('127.0.0.1',5143)\n",
")\n",
"server.start()\n",
"db_host = server.local_bind_host\n",
"db_port = server.local_bind_port\n",
"conn = pymysql.connect(\n",
" host=db_host,\n",
" port=db_port,\n",
" user=\"root\",\n",
" password=\"Z4rf7eZZe500dxa\",\n",
" database=f\"merge_pet_audit_1\"\n",
")\n",
"cursor = conn.cursor()\n",
"# 获取dwUid=400100082的数据\n",
"user_names = [\n",
" \"13100024182\",\n",
" \"13100024183\",\n",
" \"13100024011\",\n",
" \"13100024012\",\n",
" \"13100024081\",\n",
" \"13100024082\",\n",
" \"13100024161\",\n",
" \"13100024162\"\n",
"]\n",
"\n",
"# 先获取dwUin=400100088的原始mData和updateTime\n",
"cursor.execute(\"SELECT mData, updateTime FROM t_player_mod WHERE dwUin=400100088\")\n",
"row = cursor.fetchone()\n",
"if not row:\n",
" print(\"未找到dwUin=400100088的数据\")\n",
"else:\n",
" mData, updateTime = row\n",
" for user_name in user_names:\n",
" # 查找auto_id\n",
" cursor.execute(\"SELECT auto_id FROM t_account WHERE user_name=%s\", (user_name,))\n",
" result = cursor.fetchone()\n",
" if result:\n",
" auto_id = result[0]\n",
" # 更新t_player_mod表\n",
" cursor.execute(\n",
" \"UPDATE t_player_mod SET mData=%s, updateTime=%s WHERE dwUin=%s\",\n",
" (mData, updateTime, auto_id + 400100000)\n",
" )\n",
" print(f\"user_name={user_name}, auto_id={auto_id} 已更新\")\n",
" else:\n",
" print(f\"user_name={user_name} 未找到auto_id\")\n",
" conn.commit()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# 读取CSV文件\n",
"df = pd.read_excel('item.xlsx')\n",
"\n",
"# 将数据转换为TS的字典格式\n",
"df = df.fillna('')\n",
"ts_dict = df.to_dict(orient='records')\n",
"\n",
"# 写入新的TS文件\n",
"with open('output.ts', 'w', encoding='utf-8') as f:\n",
" f.write('export const data = ')\n",
" f.write(str(ts_dict))\n",
" f.write(';')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"def get_location_by_ip(ip):\n",
" url = f\"http://ip-api.com/json/{ip}?lang=zh-CN\"\n",
" response = requests.get(url)\n",
" data = response.json()\n",
" if data['status'] == 'success':\n",
" return {\n",
" '国家': data['country'],\n",
" '省份': data['regionName'],\n",
" '城市': data['city'],\n",
" '运营商': data['isp'],\n",
" '经度': data['lon'],\n",
" '纬度': data['lat']\n",
" }\n",
" else:\n",
" return None\n",
"\n",
"# 示例\n",
"ip = '8.8.8.8'\n",
"location = get_location_by_ip(ip)\n",
"print(location)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"`g.v=aE|-S}[Uk\"^\n"
]
}
],
"source": [
"import random\n",
"import string\n",
"\n",
"def generate_random_string(length):\n",
" # 定义要使用的字符集\n",
" characters = string.ascii_letters + string.digits + string.punctuation\n",
" # 生成随机字符串\n",
" random_string = ''.join(random.choice(characters) for i in range(length))\n",
" return random_string\n",
"\n",
"# 示例:生成一个长度为 12 的随机字符串\n",
"random_string = generate_random_string(16)\n",
"print(random_string)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"日期时间: 2025-02-23 08:00:00\n"
]
}
],
"source": [
"from datetime import datetime\n",
"\n",
"# 示例时间戳\n",
"timestamp = 1740268800\n",
"\n",
"# 将时间戳转换为日期时间\n",
"dt_object = datetime.fromtimestamp(timestamp)\n",
"\n",
"# 打印日期时间\n",
"print(\"日期时间:\", dt_object)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1739428748\n"
]
}
],
"source": [
"import time\n",
"\n",
"timestamp = int(time.time())\n",
"print(timestamp)\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2025-04-28 10:59:46 BST\n"
]
}
],
"source": [
"from datetime import datetime\n",
"import pytz\n",
"\n",
"date = datetime.now()\n",
"timezone = pytz.timezone('Europe/London')\n",
"aware_date = timezone.localize(date)\n",
"formatted_date = aware_date.strftime(\"%Y-%m-%d %H:%M:%S %Z\")\n",
"print(formatted_date)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import chardet\n",
"\n",
"# 获取当前目录下的所有文件\n",
"files = [f for f in os.listdir('F:\\Github\\aplus-b_-pet_-c_nation') if os.path.isfile(f)]\n",
"\n",
"# 检测每个文件的编码\n",
"for file in files:\n",
" with open(file, 'rb') as f:\n",
" raw_data = f.read()\n",
" result = chardet.detect(raw_data)\n",
" print(f\"文件: {file}, 编码: {result['encoding']}, 置信度: {result['confidence']}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"状态码: 404\n",
"返回文本: 404 page not found\n"
]
}
],
"source": [
"import requests\n",
"# 使用已在其它单元导入的 requests 发起 POST 请求\n",
"url = \"http://ship.bywaystudios.com/api/tuyou/charge\"\n",
"\n",
"payload = {\n",
" \"amount\": 100, # 示例字段,根据接口规范调整\n",
" \"currency\": \"USD\",\n",
" \"description\": \"test charge\",\n",
" \"metadata\": {\"order_id\": \"test123\"}\n",
"}\n",
"\n",
"headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" # 如果接口需要认证,加入相应头,例如:\n",
" # \"Authorization\": \"Bearer YOUR_TOKEN\"\n",
"}\n",
"\n",
"try:\n",
" resp = requests.post(url, json=payload, headers=headers, timeout=10)\n",
" print(\"状态码:\", resp.status_code)\n",
" # 尝试解析为 JSON否则打印文本\n",
" try:\n",
" print(\"返回 JSON:\", resp.json())\n",
" except ValueError:\n",
" print(\"返回文本:\", resp.text)\n",
"except requests.RequestException as e:\n",
" print(\"请求失败:\", e)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\U\\AppData\\Roaming\\Python\\Python313\\site-packages\\openpyxl\\styles\\stylesheet.py:237: UserWarning: Workbook contains no default style, apply openpyxl's default\n",
" warn(\"Workbook contains no default style, apply openpyxl's default\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"已输出 Excel: merged.xlsx\n",
"已输出 CSV: merged.csv\n"
]
}
],
"source": [
"import os\n",
"import glob\n",
"import pandas as pd\n",
"import string\n",
"\n",
"# 合并当前目录下所有 .xlsx 文件中名为 sheet0..sheet38 的子表到一个表\n",
"\n",
"sheet_names = [f\"Sheet{i}\" for i in range(39)] # sheet0..sheet38\n",
"\n",
"def merge_sheets_in_dir(path=\".\", pattern=\"*.xlsx\", out_excel=\"merged.xlsx\", out_csv=\"merged.csv\", custom_columns=None):\n",
" \"\"\"\n",
" 合并多个Excel文件的指定sheet\n",
" \n",
" Args:\n",
" custom_columns: dict, 列名映射,例如 {\"A\": \"姓名\", \"B\": \"年龄\"}\n",
" \"\"\"\n",
" files = [f for f in glob.glob(os.path.join(path, pattern)) if not os.path.basename(f).startswith(\"~$\")]\n",
" dfs = []\n",
" for file in files:\n",
" try:\n",
" # 读取时不使用第一行作为列名自动生成A,B,C...列名\n",
" xls = pd.read_excel(file, sheet_name=None, header=None)\n",
" except Exception as e:\n",
" print(f\"跳过文件 {file},读取失败: {e}\")\n",
" continue\n",
"\n",
" for s in sheet_names:\n",
" if s in xls and isinstance(xls[s], pd.DataFrame):\n",
" df = xls[s].copy()\n",
" \n",
" # 生成A,B,C...列名\n",
" num_cols = len(df.columns)\n",
" col_letters = [string.ascii_uppercase[i] if i < 26 else f\"{string.ascii_uppercase[i//26-1]}{string.ascii_uppercase[i%26]}\" for i in range(num_cols)]\n",
" df.columns = col_letters\n",
" \n",
" # 应用自定义列名\n",
" if custom_columns:\n",
" df = df.rename(columns=custom_columns)\n",
" \n",
" # 添加来源信息,便于追溯\n",
" df[\"_source_file\"] = os.path.basename(file)\n",
" df[\"_sheet_name\"] = s\n",
" dfs.append(df)\n",
" # 如果不存在则跳过\n",
"\n",
" if not dfs:\n",
" print(\"未找到任何匹配的 sheet未生成合并文件。\")\n",
" return None\n",
"\n",
" # 合并,保留所有列(列并集)\n",
" merged = pd.concat(dfs, ignore_index=True, sort=False)\n",
"\n",
" # 写出文件\n",
" try:\n",
" merged.to_excel(out_excel, index=False)\n",
" print(f\"已输出 Excel: {out_excel}\")\n",
" except Exception as e:\n",
" print(f\"写 Excel 失败: {e}\")\n",
"\n",
" try:\n",
" merged.to_csv(out_csv, index=False)\n",
" print(f\"已输出 CSV: {out_csv}\")\n",
" except Exception as e:\n",
" print(f\"写 CSV 失败: {e}\")\n",
"\n",
" return merged\n",
"\n",
"# 执行合并(可修改 path 或 输出文件名)\n",
"# 示例自定义列名现在A,B,C对应实际的列位置\n",
"column_mapping = {\n",
" \"A\": \"用户ID\",\n",
" \"B\": \"用户名\", \n",
" \"C\": \"积分\"\n",
"}\n",
"merged_df = merge_sheets_in_dir(\n",
" path=r\"D:\\Github\\devops\\source\",\n",
" custom_columns=column_mapping # 传入None则不重命名\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"连接哨兵或 Redis 时出错: No master found for 'mymaster' : <redis.client.Redis(<redis.connection.ConnectionPool(<redis.connection.Connection(host=127.0.0.1,port=26379,db=0)>)>)> - TimeoutError('Timeout connecting to server')\n"
]
}
],
"source": [
"from redis.sentinel import Sentinel\n",
"\n",
"# 连接 Redis 哨兵并获取主节点的示例\n",
"# 若未安装 redis 库,请先在终端运行: pip install redis\n",
"\n",
"\n",
"# 修改为你的哨兵地址列表和 master 名称(若只有一个哨兵地址,仍以列表形式传入)\n",
"SENTINELS = [('127.0.0.1', 26379)]\n",
"MASTER_NAME = 'mymaster' # 根据你的配置修改(常见默认为 'mymaster'\n",
"\n",
"# 如果哨兵或 Redis 需要密码,按需设置以下两个变量\n",
"SENTINEL_PASSWORD = None # 如果 sentinel 自身需要认证,设置该值或保持 None\n",
"REDIS_PASSWORD = None # 如果 redis 主从需要认证,设置该值或保持 None\n",
"\n",
"try:\n",
" sentinel = Sentinel(\n",
" SENTINELS,\n",
" socket_timeout=1,\n",
" sentinel_kwargs={'password': SENTINEL_PASSWORD} if SENTINEL_PASSWORD else None\n",
" )\n",
"\n",
" # 获取主节点客户端(写操作走主节点)\n",
" master = sentinel.master_for(MASTER_NAME, socket_timeout=1, password=REDIS_PASSWORD)\n",
" # 获取主节点的 IP 地址和端口\n",
" master_info = sentinel.discover_master(MASTER_NAME)\n",
" print(f\"主节点地址: {master_info[0]}:{master_info[1]}\")\n",
" # 获取从节点客户端(读操作走从节点)\n",
" slave = sentinel.slave_for(MASTER_NAME, socket_timeout=1, password=REDIS_PASSWORD)\n",
"\n",
" # 简单读写测试\n",
" master.set('sentinel_test_key', 'hello')\n",
" val = slave.get('sentinel_test_key') # 视复制延迟,可能需要短暂等待\n",
" if val is not None:\n",
" print(\"从节点读取值:\", val.decode() if isinstance(val, bytes) else val)\n",
" else:\n",
" print(\"从节点未读到值(可能有复制延迟),主节点值:\", master.get('sentinel_test_key'))\n",
"\n",
"except Exception as e:\n",
" print(\"连接哨兵或 Redis 时出错:\", e)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"原始主节点: 172.22.0.2:6379\n",
"映射后地址: 172.22.0.2:6379\n",
"连接失败: Timeout connecting to server\n",
"直接端口映射连接成功\n",
"使用直接端口映射连接\n",
"连接失败: Timeout connecting to server\n",
"直接端口映射连接成功\n",
"使用直接端口映射连接\n"
]
}
],
"source": [
"from redis.sentinel import Sentinel\n",
"import redis\n",
"\n",
"# 解决容器内IP映射问题的几种方案\n",
"\n",
"# 方案1: 地址映射 - 将容器内部IP映射到外部可访问地址\n",
"IP_MAPPING = {\n",
" '172.17.0.2': '127.0.0.1', # 容器内IP -> 外部IP\n",
" '172.17.0.3': '127.0.0.1', # 根据实际情况修改\n",
" # 添加更多映射...\n",
"}\n",
"\n",
"# 方案2: 自定义连接类处理IP映射\n",
"class MappedSentinel(Sentinel):\n",
" def __init__(self, sentinels, ip_mapping=None, **kwargs):\n",
" super().__init__(sentinels, **kwargs)\n",
" self.ip_mapping = ip_mapping or {}\n",
" \n",
" def master_for(self, service_name, **kwargs):\n",
" # 获取原始主节点信息\n",
" master_info = self.discover_master(service_name)\n",
" original_host = master_info[0]\n",
" port = master_info[1]\n",
" \n",
" # 应用IP映射\n",
" mapped_host = self.ip_mapping.get(original_host, original_host)\n",
" print(f\"原始主节点: {original_host}:{port}\")\n",
" print(f\"映射后地址: {mapped_host}:{port}\")\n",
" \n",
" # 直接创建Redis连接到映射后的地址\n",
" return redis.Redis(host=mapped_host, port=port, **kwargs)\n",
"\n",
"# 使用示例\n",
"SENTINELS = [('127.0.0.1', 6381)]\n",
"MASTER_NAME = 'mymaster'\n",
"\n",
"try:\n",
" # 方案1: 使用自定义Sentinel类\n",
" sentinel = MappedSentinel(\n",
" SENTINELS,\n",
" ip_mapping=IP_MAPPING,\n",
" socket_timeout=1\n",
" )\n",
" \n",
" master = sentinel.master_for(MASTER_NAME, socket_timeout=1, password=None)\n",
" \n",
" # 测试连接\n",
" master.set('test_key', 'test_value')\n",
" result = master.get('test_key')\n",
" print(f\"连接测试成功: {result}\")\n",
" \n",
"except Exception as e:\n",
" print(\"连接失败:\", e)\n",
"\n",
"# 方案3: 直接指定端口映射如果使用Docker端口映射\n",
"def connect_with_port_mapping():\n",
" \"\"\"\n",
" 如果Redis容器使用了端口映射如 -p 6379:6379\n",
" 可以直接连接到映射的端口\n",
" \"\"\"\n",
" try:\n",
" # 假设容器映射到本地端口6379\n",
" direct_redis = redis.Redis(host='127.0.0.1', port=6379, socket_timeout=1, password='change_me_redis_pass')\n",
" direct_redis.ping()\n",
" print(\"直接端口映射连接成功\")\n",
" return direct_redis\n",
" except Exception as e:\n",
" print(f\"直接连接失败: {e}\")\n",
" return None\n",
"\n",
"# 方案4: 环境变量配置\n",
"import os\n",
"def get_redis_config():\n",
" \"\"\"\n",
" 从环境变量获取Redis配置\n",
" \"\"\"\n",
" return {\n",
" 'host': os.getenv('REDIS_HOST', '127.0.0.1'),\n",
" 'port': int(os.getenv('REDIS_PORT', '6379')),\n",
" 'password': os.getenv('REDIS_PASSWORD', None)\n",
" }\n",
"\n",
"# 测试直接连接\n",
"direct_connection = connect_with_port_mapping()\n",
"if direct_connection:\n",
" print(\"使用直接端口映射连接\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 补充Docker网络配置建议\n",
"\n",
"docker_tips = \"\"\"\n",
"解决容器IP问题的Docker配置建议\n",
"\n",
"1. 端口映射方式:\n",
" docker run -p 6379:6379 redis:latest\n",
"\n",
"2. 自定义网络:\n",
" docker network create redis-net\n",
" docker run --network redis-net --name redis-master redis:latest\n",
"\n",
"3. Host网络模式:\n",
" docker run --network host redis:latest\n",
"\n",
"4. 环境变量配置:\n",
" docker run -e REDIS_HOST=127.0.0.1 -e REDIS_PORT=6379 your-app\n",
"\n",
"5. Docker Compose示例:\n",
" version: '3'\n",
" services:\n",
" redis:\n",
" image: redis:latest\n",
" ports:\n",
" - \"6379:6379\"\n",
" app:\n",
" build: .\n",
" environment:\n",
" - REDIS_HOST=redis\n",
" - REDIS_PORT=6379\n",
" depends_on:\n",
" - redis\n",
"\"\"\"\n",
"\n",
"print(docker_tips)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"/usr/local/game/logs/ga_log/.log"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}