使用Python请求复制Curl命令

时间:2013-09-04 16:01:46

标签: python curl python-requests

将以下内容转换为Python请求调用的语法是什么?

curl -F file=@/your/data.csv http://host/ingest

我尝试了以下内容:

 files = {'file': ('injestable_file', open(temp_file, 'rb'))}
 requests.post(url,files=files)

但这不起作用,但相应的curl命令确实有效。

1 个答案:

答案 0 :(得分:2)

您不能显示确切的代码,因为您提供的curl命令和您提供的代码有效。看我的输出:

~ curl -F foo=bar -F file=@setup.py 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=\"graffatcolmingov@gmail.com\",\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='me@kennethreitz.com',\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文字之外,你的代码应该可以正常工作。

请修改您的帖子,其中包含更多详细信息,包括您正在使用的请求版本以及有关您尝试发布的数据的详细信息。当您发布的内容完全正确时,我们无法帮助您。