我想通过Python发送HTTP POST请求。已通过pip3 install requests
安装requests
模块,并显示"需求已满足",因此已安装。
我正在使用Python版本3.8.0
。
代码:
import requests as r
headers = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
url = 'http://localhost:8083/push/message'
jsons = {"test"}
r.post(url, json=jsons, headers=headers)
错误:
Traceback (most recent call last):
File "http.py", line 1, in <module>
import requests as r
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\__init__.py", line 43, in <module>
import urllib3
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 11, in <module>
from .exceptions import (
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap>", line 618, in _load_backward_compatible
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 199, in load_module
mod = mod._resolve()
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 113, in _resolve
return _import_module(self.mod)
File "C:\User\name\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\packages\six.py", line 82, in _import_module
__import__(name)
File "C:\scripts\http.py", line 5, in <module>
r.post(url, json=jsons, headers=headers)
AttributeError: partially initialized module 'requests' has no attribute 'post' (most likely due to a circular import)
当我关闭命令行并启动一个新的命令行,然后进入 python
,我可以导入它:
C:\Windows\system32>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.post
<function post at 0x000001A4F7B9D310>
>>>
执行脚本时,有时会发生相同的情况(仅有时)。 (顺便说一句,当它正常工作时,它也像应该那样发布到服务器)
是否有人遇到过类似的问题,并可能提出解决方案? 顺便说一下,Python 3是我在这台机器上安装的唯一Python版本 - 但在其他计算机上也面临着类似的问题!