debug_buy_502.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """
  2. Steam Market Buy API 调试脚本
  3. 复现 502 错误场景
  4. 日志信息:
  5. - URL: https://steamcommunity.com/market/buylisting/634586007392692709
  6. - listing_id: 634586007392692709
  7. - goods_id: 35820
  8. - goods_name: 新星 | 毒蜥 (略有磨损)
  9. - steam_account: cea10388
  10. - target_price_usd: 5.5
  11. """
  12. import requests
  13. import urllib.parse
  14. import json
  15. from datetime import datetime
  16. # 请求配置
  17. LISTING_ID = "689755100902207377"
  18. SESSION_ID = "C859D89325CCF1A3963504BBA006038D"
  19. STEAM_LOGIN_SECURE = (
  20. "76561199354580595%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0."
  21. "eyAiaXNzIjogInI6MDAwRl8yODA5QzM3N182NERDMyIsICJzdWIiOiAiNzY1NjExOTkzNTQ1ODA1OTUiLCAiYXVkIjogWyAiY2xpZW50IiwgIndlYiIgXSwgImV4cCI6IDE3NzY2MTA2NDAsICJuYmYiOiAxNzY3ODgzNjc1LCAiaWF0IjogMTc3NjUyMzY3NSwgImp0aSI6ICIwMDEyXzI4MDlDMUQ0X0I0MEM0IiwgIm9hdCI6IDE3NzY1MjM2NzUsICJydF9leHAiOiAxNzc5MTMwMjMyLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTAzLjEwNi4xODguMTU4IiwgImlwX2NvbmZpcm1lciI6ICIxMDMuMTA2LjE4OC4xNTgiIH0."
  22. "UcD-w8xcSCRFQKS6CDLzAiRocQzW0UX54O8HwAXv_hm8ULiUvgdQYdZe2eZeqw_Hel66Zf9T5uNgJSbQNzogDw;"
  23. )
  24. ITEM_NAME = "Negev%20%7C%20Wall%20Bang%20%28Minimal%20Wear%29"
  25. # 价格信息(从日志中获取)
  26. SUBTOTAL = "21"
  27. FEE = "3"
  28. TOTAL = "24"
  29. def build_cookie() -> str:
  30. """构建完整的 Cookie 字符串"""
  31. return (
  32. f"timezoneOffset=28800,0; "
  33. f"sessionid={SESSION_ID}; "
  34. f"steamLoginSecure={STEAM_LOGIN_SECURE}"
  35. )
  36. def build_request_body() -> dict:
  37. """构建请求体"""
  38. return {
  39. "sessionid": SESSION_ID,
  40. "currency": "1",
  41. "subtotal": SUBTOTAL,
  42. "fee": FEE,
  43. "total": TOTAL,
  44. "quantity": "1",
  45. "save_my_address": "0",
  46. "tradefee_tax": "0",
  47. "confirmation": "0",
  48. "billing_state": "",
  49. }
  50. def build_headers() -> dict:
  51. """构建请求头"""
  52. return {
  53. "origin": "https://steamcommunity.com",
  54. "cookie": build_cookie(),
  55. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
  56. "accept": "*/*",
  57. "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  58. "referer": f"https://steamcommunity.com/market/listings/730/{ITEM_NAME}",
  59. }
  60. def send_buy_request(
  61. listing_id: str,
  62. use_proxy: bool = False,
  63. proxy_url: str = None,
  64. session: requests.Session = None,
  65. ) -> tuple[requests.Response, float]:
  66. """
  67. 发送购买请求
  68. Returns:
  69. (response, elapsed_ms)
  70. """
  71. url = f"https://steamcommunity.com/market/buylisting/{listing_id}"
  72. data = build_request_body()
  73. headers = build_headers()
  74. if session is None:
  75. session = requests.Session()
  76. proxies = None
  77. if use_proxy and proxy_url:
  78. proxies = {"http": proxy_url, "https": proxy_url}
  79. start_time = datetime.now()
  80. try:
  81. response = session.post(
  82. url,
  83. data=data,
  84. headers=headers,
  85. proxies=proxies,
  86. timeout=30,
  87. allow_redirects=False,
  88. )
  89. except requests.exceptions.RequestException as e:
  90. elapsed = (datetime.now() - start_time).total_seconds() * 1000
  91. print(f"[ERROR] 请求异常: {e}")
  92. raise
  93. elapsed_ms = (datetime.now() - start_time).total_seconds() * 1000
  94. return response, elapsed_ms
  95. def print_response_info(response: requests.Response, elapsed_ms: float):
  96. """打印响应信息"""
  97. print("\n" + "=" * 60)
  98. print(f"状态码: {response.status_code}")
  99. print(f"耗时: {elapsed_ms:.0f}ms")
  100. print(f"响应头: {json.dumps(dict(response.headers), indent=2, ensure_ascii=False)}")
  101. print(f"\n响应体:")
  102. try:
  103. body_json = response.json()
  104. print(json.dumps(body_json, indent=2, ensure_ascii=False))
  105. except json.JSONDecodeError:
  106. print(response.text)
  107. print("=" * 60)
  108. def main():
  109. print("Steam Market Buy API 调试脚本")
  110. print(f"Listing ID: {LISTING_ID}")
  111. print(f"Session ID: {SESSION_ID}")
  112. print(f"价格: subtotal={SUBTOTAL}, fee={FEE}, total={TOTAL}")
  113. print()
  114. session = requests.Session()
  115. # 测试 1: 直接请求(不使用代理)
  116. print("[测试 1] 直接请求(不使用代理)...")
  117. try:
  118. response, elapsed_ms = send_buy_request(LISTING_ID, use_proxy=False, session=session)
  119. print_response_info(response, elapsed_ms)
  120. if response.status_code == 200:
  121. print("✅ 购买成功!")
  122. elif response.status_code == 502:
  123. print("❌ 502 错误 - 他人已购买该物品")
  124. try:
  125. error_body = response.json()
  126. print(f"错误信息: {error_body.get('message', 'Unknown')}")
  127. except:
  128. print(f"错误信息: {response.text}")
  129. elif response.status_code == 429:
  130. print("⚠️ 429 错误 - 请求过于频繁")
  131. else:
  132. print(f"❌ 其他错误: {response.status_code}")
  133. except Exception as e:
  134. print(f"[ERROR] {e}")
  135. if __name__ == "__main__":
  136. main()