POSTing Pastebin API 在 Python 中创建过去并得到正确的回复,但粘贴的数据不正确
POSTing Pastebin API to create a past in Python and get a correct reply, yet the pasted data is incorrect
所以我正在尝试编写一个 POST 面向 Pastebin 的程序并创建一个新的粘贴。发布、请求等一切正常——但当我检查实际粘贴时,它只是源代码中的一个单词。我将 POSTed 数据发送到 https://httpbin.org/post,所有数据都很好,但我得到了错误的粘贴数据。
代码:
#!/usr/bin/python3
import requests
import argparse
import sys
epilog = """Usage: sourcepaste [ FILENAME ] [ LANGUAGE ] [ OPTIONS ]
Example use(s):
sourcepaste testfile.py python 2 6M
sourcepaste source.cpp cpp
sourcepaste js.js javascript 1
"""
parser = argparse.ArgumentParser(description='A CLI-based way to post on Pastebin',
prog="sourcepaste",
epilog=epilog)
parser.add_argument('source',
help='the file of your source code')
parser.add_argument('language',
help='the language of your source code | check langs.txt for all available languages')
parser.add_argument('privacy', nargs='?',
help='how private you want your post | 0 = Public, 1 = Unlisted, 2 = Private | Default = 0')
parser.add_argument('time', nargs='?',
help="""when your post should expire | N(ever), 10M(inutes), 1H(our), 1D(ay),
1W(eek), 2W(eeks), 1M(onth), 6M(onths), 1Y(ear) | Default = 1W(eek)""")
parse_args = parser.parse_args()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
with open(parse_args.source, "r") as source_code_read:
source_code_content = source_code_read.read()
if (len(sys.argv) - 1) == 2:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date=1W&api_paste_private=0&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
elif (len(sys.argv) - 1) == 3:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date=1W&api_paste_private={parse_args.privacy}&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
elif (len(sys.argv) - 1) == 4:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date={parse_args.time}&api_paste_private={parse_args.privacy}&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
pageget = requests.post("https://pastebin.com/api/api_post.php", headers=headers, data=data)
print(pageget.content)
我的 shell 输入:
./sourcepaste test.py python 2 N
回复示例:
https://pastebin.com/IuHbDt
link 的内容:
{test_content}
- 没有其他代码
希望得到一些帮助;我很奇怪 POST 数据是正确的,回复很好,但实际粘贴不正确。
谢谢!
您的错误是试图将 data
收集到一个字符串中。它应该是一个命令。另一方面,它运作良好。但是,它存在一些技术问题。例如,您将 POST
参数收集到 data
的方式可以做得更好。但我觉得这不是你提出问题的原因。
所以我稍微修改了你的解决方案,现在可以了:
import requests
import argparse
import sys
API_DEV_KEY = "<MY_API_DEV_KEY>"
epilog = """Usage: sourcepaste [ FILENAME ] [ LANGUAGE ] [ OPTIONS ]
Example use(s):
sourcepaste testfile.py python 2 6M
sourcepaste source.cpp cpp
sourcepaste js.js javascript 1
"""
parser = argparse.ArgumentParser(description='A CLI-based way to post on Pastebin',
prog="sourcepaste",
epilog=epilog)
parser.add_argument('source',
help='the file of your source code')
parser.add_argument('language',
help='the language of your source code | check langs.txt for all available languages')
parser.add_argument('privacy', nargs='?',
help='how private you want your post | 0 = Public, 1 = Unlisted, 2 = Private | Default = 0')
parser.add_argument('time', nargs='?',
help="""when your post should expire | N(ever), 10M(inutes), 1H(our), 1D(ay),
1W(eek), 2W(eeks), 1M(onth), 6M(onths), 1Y(ear) | Default = 1W(eek)""")
parse_args = parser.parse_args()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
with open(parse_args.source, "r") as source_code_read:
source_code_content = source_code_read.read()
data = None
if (len(sys.argv) - 1) == 2:
data = {
"api_dev_key": API_DEV_KEY,
"api_paste_code": source_code_content,
"api_option": "paste",
"api_paste_expire_date": "1W",
"api_paste_private": "0",
"api_paste_name": f"{parse_args.source}@{parse_args.language}",
"api_paste_format": parse_args.language,
}
if data:
response = requests.post("https://pastebin.com/api/api_post.php", headers=headers, data=data)
print(response.content)
结果:
您可以看到,我只针对两个参数修改了您的脚本:source
和 language
。我毫不怀疑,现在你可以自己添加所有你需要的额外参数。
所以我正在尝试编写一个 POST 面向 Pastebin 的程序并创建一个新的粘贴。发布、请求等一切正常——但当我检查实际粘贴时,它只是源代码中的一个单词。我将 POSTed 数据发送到 https://httpbin.org/post,所有数据都很好,但我得到了错误的粘贴数据。
代码:
#!/usr/bin/python3
import requests
import argparse
import sys
epilog = """Usage: sourcepaste [ FILENAME ] [ LANGUAGE ] [ OPTIONS ]
Example use(s):
sourcepaste testfile.py python 2 6M
sourcepaste source.cpp cpp
sourcepaste js.js javascript 1
"""
parser = argparse.ArgumentParser(description='A CLI-based way to post on Pastebin',
prog="sourcepaste",
epilog=epilog)
parser.add_argument('source',
help='the file of your source code')
parser.add_argument('language',
help='the language of your source code | check langs.txt for all available languages')
parser.add_argument('privacy', nargs='?',
help='how private you want your post | 0 = Public, 1 = Unlisted, 2 = Private | Default = 0')
parser.add_argument('time', nargs='?',
help="""when your post should expire | N(ever), 10M(inutes), 1H(our), 1D(ay),
1W(eek), 2W(eeks), 1M(onth), 6M(onths), 1Y(ear) | Default = 1W(eek)""")
parse_args = parser.parse_args()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
with open(parse_args.source, "r") as source_code_read:
source_code_content = source_code_read.read()
if (len(sys.argv) - 1) == 2:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date=1W&api_paste_private=0&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
elif (len(sys.argv) - 1) == 3:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date=1W&api_paste_private={parse_args.privacy}&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
elif (len(sys.argv) - 1) == 4:
data = f"api_dev_key=<my-dev-key>&api_paste_code={source_code_content}&api_option=paste&api_user_key=<my-user-key>&api_paste_expire_date={parse_args.time}&api_paste_private={parse_args.privacy}&api_paste_name={parse_args.source}@{parse_args.language}&api_paste_format={parse_args.language}"
pageget = requests.post("https://pastebin.com/api/api_post.php", headers=headers, data=data)
print(pageget.content)
我的 shell 输入:
./sourcepaste test.py python 2 N
回复示例:
https://pastebin.com/IuHbDt
link 的内容:
{test_content}
- 没有其他代码
希望得到一些帮助;我很奇怪 POST 数据是正确的,回复很好,但实际粘贴不正确。
谢谢!
您的错误是试图将 data
收集到一个字符串中。它应该是一个命令。另一方面,它运作良好。但是,它存在一些技术问题。例如,您将 POST
参数收集到 data
的方式可以做得更好。但我觉得这不是你提出问题的原因。
所以我稍微修改了你的解决方案,现在可以了:
import requests
import argparse
import sys
API_DEV_KEY = "<MY_API_DEV_KEY>"
epilog = """Usage: sourcepaste [ FILENAME ] [ LANGUAGE ] [ OPTIONS ]
Example use(s):
sourcepaste testfile.py python 2 6M
sourcepaste source.cpp cpp
sourcepaste js.js javascript 1
"""
parser = argparse.ArgumentParser(description='A CLI-based way to post on Pastebin',
prog="sourcepaste",
epilog=epilog)
parser.add_argument('source',
help='the file of your source code')
parser.add_argument('language',
help='the language of your source code | check langs.txt for all available languages')
parser.add_argument('privacy', nargs='?',
help='how private you want your post | 0 = Public, 1 = Unlisted, 2 = Private | Default = 0')
parser.add_argument('time', nargs='?',
help="""when your post should expire | N(ever), 10M(inutes), 1H(our), 1D(ay),
1W(eek), 2W(eeks), 1M(onth), 6M(onths), 1Y(ear) | Default = 1W(eek)""")
parse_args = parser.parse_args()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
with open(parse_args.source, "r") as source_code_read:
source_code_content = source_code_read.read()
data = None
if (len(sys.argv) - 1) == 2:
data = {
"api_dev_key": API_DEV_KEY,
"api_paste_code": source_code_content,
"api_option": "paste",
"api_paste_expire_date": "1W",
"api_paste_private": "0",
"api_paste_name": f"{parse_args.source}@{parse_args.language}",
"api_paste_format": parse_args.language,
}
if data:
response = requests.post("https://pastebin.com/api/api_post.php", headers=headers, data=data)
print(response.content)
结果:
您可以看到,我只针对两个参数修改了您的脚本:source
和 language
。我毫不怀疑,现在你可以自己添加所有你需要的额外参数。