我在 python 脚本中看到一些奇怪的行为,该脚本在 Google App Engine 上运行时通过 https 代理向 api 发送 post 请求。
main.py
import random
import requests
lines = open(r'proxies.txt').read().splitlines()
# finds a random proxy to use
while True:
line =random.choice(lines)
if line.split(':')[2] != "United States":
continue
proxy = line.split(':')[0] + ':' + line.split(':')[1]
website_url = 'https://api.ipify.org/?format=json'
proxy_dict = {
'https': f'https://{proxy}'
}
try:
response = requests.get(website_url, proxies=proxy_dict, timeout=2, verify=False)
print(f'\x1b[38;5;47mProxy {proxy} is working for HTTPS.')
print("current IP: " , response.text , "\n\x1b[1;0m")
break
except requests.exceptions.RequestException as e:
print(f'\x1b[38;5;3mError occurred while testing proxy {proxy}: {str(e)}\x1b[1;0m')
url = "https://prd-usta-kube-tournaments.clubspark.pro"
headers = {
"Content-Type": "application/json",
"Host": "prd-usta-kube-tournaments.clubspark.pro",
"Sec-Ch-Ua": "\"Chromium\";v=\"123\", \"Not:A-Brand\";v=\"8\"",
"Accept": "*/*",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.88 Safari/537.36",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Origin": "https://playtennis.usta.com",
"Referer": "https://playtennis.usta.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"Priority": "u=1, i"
}
payload = {
"operationName": "GetTournament",
"variables": {
"id": f"55185A9E-BBC9-4845-85B6-0C9140B00913",
"previewMode": "false"
},
"query": "query GetTournament($id: UUID!, $previewMode: Boolean) {\n publishedTournament(id: $id, previewMode: $previewMode) {\n id\n featureSettings {\n enabled\n featureId\n overridable\n __typename\n }\n commsSettings {\n emailReplyTo\n phoneNumber\n __typename\n }\n sanctionStatus\n name\n isPublished\n cancelledAt\n isCancelled\n tournamentIsFreeOfCharge\n selectionsPublished\n identificationCode\n finalisedAndCharged\n organisation {\n id\n name\n __typename\n }\n registrationRestrictions {\n entriesCloseDate\n entriesOpenDate\n entriesCloseTime\n entriesOpenTime\n timeZone\n entriesCloseDateTime\n entriesOpenDateTime\n secondsUntilEntriesClose\n secondsUntilEntriesOpen\n maxEventEntriesPerUser\n maxSinglesEntriesPerUser\n maxDoublesEntriesPerUser\n singleAgeGroupPerPlayer\n __typename\n }\n director {\n id\n firstName\n lastName\n __typename\n }\n officials(officialType: REFEREE) {\n officialType\n status\n commsSettings {\n emailReplyTo\n __typename\n }\n contact {\n id\n firstName\n lastName\n userId\n __typename\n }\n __typename\n }\n websiteContent {\n logoPath\n photoPath\n tournamentDetails\n aboutTheOrganiser\n entryInformation\n hideDraws\n hidePlayerList\n __typename\n }\n lastSanctionStatusChange(sanctionStatus: SUBMITTED) {\n createdAt\n createdByFirstName\n createdByLastName\n __typename\n }\n primaryLocation {\n id\n name\n address1\n address2\n address3\n country\n county\n latitude\n longitude\n postcode\n town\n __typename\n }\n timings {\n startDate\n endDate\n timeZone\n startDateTime\n __typename\n }\n level {\n __typename\n branding\n id\n name\n category\n orderIndex\n shortName\n }\n onlineRegistrationEnabled\n publishedEvents(previewMode: $previewMode) {\n id\n sanctionStatus\n courtLocation\n sanctionStatus\n isPublished\n formatConfiguration {\n entriesLimit\n ballColour\n drawSize\n eventFormat\n scoreFormat\n selectionProcess\n __typename\n }\n level {\n __typename\n id\n name\n category\n orderIndex\n shortName\n }\n division {\n __typename\n ballColour\n ageCategory {\n __typename\n minimumAge\n maximumAge\n todsCode\n type\n }\n eventType\n gender\n wheelchairRating\n familyType\n ratingCategory {\n __typename\n ratingCategoryType\n ratingType\n value\n minimumValue\n maximumValue\n }\n }\n pricing {\n entryFee {\n amount\n currency\n __typename\n }\n __typename\n }\n registrations {\n id\n player {\n customId {\n key\n value\n __typename\n }\n firstName\n lastName\n name\n gender\n __typename\n }\n registrationDate\n selectionIndex\n selectionStatus\n __typename\n }\n surface\n timings {\n startDateTime\n __typename\n }\n tournament {\n id\n registrationRestrictions {\n entriesCloseDateTime\n __typename\n }\n levelConfiguration {\n skillLevel\n __typename\n }\n __typename\n }\n withdrawals {\n player {\n customId {\n key\n value\n __typename\n }\n customIds {\n key\n value\n __typename\n }\n firstName\n lastName\n name\n gender\n __typename\n }\n registrationDate\n __typename\n }\n teamEventConfiguration {\n isDominantDuo\n maximumTeams\n eventFormat\n selectionProcess\n __typename\n }\n __typename\n }\n __typename\n }\n}"
}
cookies = {}
r = requests.get(url, json=payload, headers=headers, cookies=cookies, verify=False, proxies=proxy_dict)
response.encoding = 'utf-8' # Ensure correct encoding
print(r.text)
代理.txt:
84.239.14.169:9002:United States
当代码在本地运行时,它完全没问题,但是,当我在 Google App Engine 上运行代码时,它会打印出这样的随机字符
��������tu�S7���.��JU�,ӳ"#<,��!!A�L��:����#�;��r�6��)�Vj���<hh(����O�RAA�����3Q��0>K.����0�
我最初认为 GAE 上不允许代理连接,直到我意识到检查代理是否正常工作的调用“https://api.ipify.org/?format=json”正在响应完美的数据。我还在其他网站(例如 https://example.com)测试了代码,它以正常数据响应 GAE
我知道我正在尝试访问的API(https://prd-usta-kube-tournaments.clubspark.pro)确实有cloudflare保护并且是graphql
问题出在接受编码上。我删除了 br,它工作得很好 ty :)