将以下内容转换为Python请求调用的语法是什么?
curl -F file=@/your/data.csv http://host/ingest
我尝试了以下方法:
files = {'file': ('injestable_file', open(temp_file, 'rb'))}
requests.post(url,files=files)
但这不起作用,但是相应的curl命令起作用。
最佳答案
您不得显示您的确切代码,因为您提供的curl命令和您提供的代码可以正常工作。看到我的输出:
~ curl -F foo=bar -F [email protected] https://httpbin.org/post
{
"headers": {
"Accept": "*/*",
"Content-Length": "2370",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "curl/7.29.0",
"Content-Type": "multipart/form-data; boundary=----------------------------959364026805"
},
"files": {
"file": "#!/usr/bin/env python\n\nimport sys\nimport os\nimport re\n\nkwargs = {}\nrequires = []\npackages = [\n \"github3\",\n \"github3.gists\",\n \"github3.repos\",\n \"github3.issues\",\n]\n\ntry:\n from setuptools import setup\n kwargs['test_suite'] = 'run_tests.collect_tests'\n kwargs['tests_require'] = ['mock', 'expecter', 'coverage==3.5.2']\n packages.append('tests')\nexcept ImportError:\n from distutils.core import setup # NOQA\n\nif sys.argv[-1] in (\"submit\", \"publish\"):\n os.system(\"python setup.py sdist upload\")\n sys.exit()\n\nrequires.extend([\"requests >= 1.2.3\", \"uritemplate.py >= 0.2.0\"])\n\n__version__ = ''\nwith open('github3/__init__.py', 'r') as fd:\n reg = re.compile(r'__version__ = [\\'\"]([^\\'\"]*)[\\'\"]')\n for line in fd:\n m = reg.match(line)\n if m:\n __version__ = m.group(1)\n break\n\nif not __version__:\n raise RuntimeError('Cannot find version information')\n\nsetup(\n name=\"github3.py\",\n version=__version__,\n description=(\"Python wrapper for the GitHub API\"\n \"(http://developer.github.com/v3)\"),\n long_description=\"\\n\\n\".join([open(\"README.rst\").read(),\n open(\"HISTORY.rst\").read()]),\n license=open('LICENSE').read(),\n author=\"Ian Cordasco\",\n author_email=\"[email protected]\",\n url=\"https://github3py.readthedocs.org\",\n packages=packages,\n package_data={'': ['LICENSE', 'AUTHORS.rst']},\n include_package_data=True,\n install_requires=requires,\n classifiers=[\n 'Development Status :: 5 - Production/Stable',\n 'License :: OSI Approved',\n 'Intended Audience :: Developers',\n 'Programming Language :: Python',\n 'Programming Language :: Python :: 2',\n 'Programming Language :: Python :: 2.6',\n 'Programming Language :: Python :: 2.7',\n 'Programming Language :: Python :: 3',\n 'Programming Language :: Python :: 3.2',\n 'Programming Language :: Python :: 3.3',\n 'Programming Language :: Python :: Implementation :: CPython',\n ],\n **kwargs\n)\n"
},
"origin": "...",
"form": {
"foo": "bar"
},
"url": "http://httpbin.org/post",
"data": "",
"args": {},
"json": null
从交互式python shell中:
>>> import pprint as pp
>>> import requests
>>> pp.pprint(requests.post('https://httpbin.org/post', files={'file': ('filename', fd)}).json())
{u'args': {},
u'data': u'',
u'files': {u'file': u"#!/usr/bin/env python\n\nimport os\nimport sys\n\nimport requests\n\ntry:\n from setuptools import setup\nexcept ImportError:\n from distutils.core import setup\n\nif sys.argv[-1] == 'publish':\n os.system('python setup.py sdist upload')\n sys.exit()\n\npackages = [\n 'requests',\n 'requests.packages',\n 'requests.packages.charade',\n 'requests.packages.urllib3',\n 'requests.packages.urllib3.packages',\n 'requests.packages.urllib3.contrib',\n 'requests.packages.urllib3.packages.ssl_match_hostname'\n]\n\nrequires = []\n\nsetup(\n name='requests',\n version=requests.__version__,\n description='Python HTTP for Humans.',\n long_description=open('README.rst').read() + '\\n\\n' +\n open('HISTORY.rst').read(),\n author='Kenneth Reitz',\n author_email='[email protected]',\n url='http://python-requests.org',\n packages=packages,\n package_data={'': ['LICENSE', 'NOTICE'], 'requests': ['*.pem']},\n package_dir={'requests': 'requests'},\n include_package_data=True,\n install_requires=requires,\n license=open('LICENSE').read(),\n zip_safe=False,\n classifiers=(\n 'Development Status :: 5 - Production/Stable',\n 'Intended Audience :: Developers',\n 'Natural Language :: English',\n 'License :: OSI Approved :: Apache Software License',\n 'Programming Language :: Python',\n 'Programming Language :: Python :: 2.6',\n 'Programming Language :: Python :: 2.7',\n 'Programming Language :: Python :: 3',\n 'Programming Language :: Python :: 3.3',\n\n ),\n)\n"},
u'form': {},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate, compress',
u'Connection': u'close',
u'Content-Length': u'1748',
u'Content-Type': u'multipart/form-data; boundary=d76a42fcaca84b3288ea4fa177bdad60',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/1.2.3 CPython/2.7.3 Linux/3.2.29'},
u'json': None,
u'origin': u'...',
u'url': u'http://httpbin.org/post'}
除了可以窥视unicode文字外,您的代码应该可以正常运行。
请编辑您的帖子,并提供更多详细信息,包括您正在使用的请求版本以及有关您要发布的数据的更多详细信息。当您发布的内容完全正确时,我们无法为您提供帮助。