Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: change the file name to 'create-ssl.py'.If 'ssl.py' is used as … #8623

Merged
merged 2 commits into from
Feb 12, 2023

Conversation

xuruidong
Copy link
Contributor

…the file name, an error occurs.AttributeError: module 'requests' has no attribute 'put'

Description

Change the file name to "create-ssl.py" in certificate.md.
If "ssl.py" is used as the file name, an error occurs.

./ssl.py LLLL.txt main.cc aa
Traceback (most recent call last):
  File "./create-ssl.py", line 6, in <module>
    import requests
  File "D:\Programs\Python\Python37\lib\site-packages\requests\__init__.py", line 43, in <module>
    import urllib3
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\__init__.py", line 11, in <module>
    from . import exceptions
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\exceptions.py", line 3, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 583, in module_from_spec
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 234, in create_module
    return self.load_module(spec.name)
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 209, in load_module
    mod = mod._resolve()
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 118, in _resolve
    return _import_module(self.mod)
  File "D:\Programs\Python\Python37\lib\site-packages\urllib3\packages\six.py", line 87, in _import_module
    __import__(name)
  File "D:\Programs\Python\Python37\lib\http\client.py", line 1341, in <module>
    import ssl
  File "ssl.py", line 17, in <module>
    resp = requests.put("http://127.0.0.1:9180/apisix/admin/ssls/1", json={
AttributeError: module 'requests' has no attribute 'put'

Fixes # (issue)

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Copy link
Member

@spacewander spacewander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a standalone test to show us why changing a filename can fix "AttributeError: module 'requests' has no attribute 'put'"?

@xuruidong
Copy link
Contributor Author

xuruidong commented Jan 10, 2023

Could you provide a standalone test to show us why changing a filename can fix "AttributeError: module 'requests' has no attribute 'put'"?

在用python 执行脚本时, 脚本的所在路径会被加入 sys.path 中。比如在脚本中加入 print ("sys.path = ", sys.path), 会得到:

 python ssl.py
sys.path =  ['E:\\xrd\\code\\tmp', 'D:\\Programs\\Python\\Python37\\python37.zip', 'D:\\Programs\\Python\\Python37\\DLLs', 'D:\\Programs\\Python\\Pyt
hon37\\lib', 'D:\\Programs\\Python\\Python37', 'D:\\Programs\\Python\\Python37\\lib\\site-packages']

E:\xrd\code\tmp 是测试脚本 ssl.py 所在的目录。
在使用 requests 进行http 请求时,会有 import ssl ( 从本次pr 的描述中的异常信息里可以看到), 这时 会在 sys.path 中查找 ssl.py 。 如果测试脚本用了相同的名字,那么import 的真正文件是 E:\xrd\code\tmp\ssl.py , 而不是系统下的 ssl.py 。
所以测试脚本不能使用 ‘ssl.py’ 这个名字,会有冲突

@xuruidong
Copy link
Contributor Author

Could you provide a standalone test to show us why changing a filename can fix "AttributeError: module 'requests' has no attribute 'put'"?

test:
use ssl.py

# python ssl.py lvh.me+1.pem lvh.me+1-key.pem lvh.me
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'warnings']
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'warnings']
Traceback (most recent call last):
  File "/usr/src/myapp/ssl.py", line 6, in <module>
    import requests
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/__init__.py", line 11, in <module>
    from . import exceptions
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/exceptions.py", line 3, in <module>
    from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/packages/six.py", line 234, in create_module
    return self.load_module(spec.name)
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/packages/six.py", line 209, in load_module
    mod = mod._resolve()
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/packages/six.py", line 118, in _resolve
    return _import_module(self.mod)
  File "/usr/src/myapp/myvenv/lib/python3.9/site-packages/urllib3/packages/six.py", line 87, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.9/http/client.py", line 1397, in <module>
    import ssl
  File "/usr/src/myapp/ssl.py", line 18, in <module>
    resp = requests.put("http://127.0.0.1:9180/apisix/admin/ssls/1", json={
AttributeError: partially initialized module 'requests' has no attribute 'put' (most likely due to a circular import)

use create-ssl.py

# mv ssl.py create-ssl.py
(myvenv) root@ap04:/usr/src/myapp# python create-ssl.py lvh.me+1.pem lvh.me+1-key.pem lvh.me
['ConnectTimeout', 'ConnectionError', 'DependencyWarning', 'FileModeWarning', 'HTTPError', 'JSONDecodeError', 'NullHandler', 'PreparedRequest', 'ReadTimeout', 'Request', 'RequestException', 'RequestsDependencyWarning', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__author_email__', '__build__', '__builtins__', '__cached__', '__cake__', '__copyright__', '__description__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__title__', '__url__', '__version__', '_check_cryptography', '_internal_utils', 'adapters', 'api', 'auth', 'certs', 'chardet_version', 'charset_normalizer_version', 'check_compatibility', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'ssl', 'status_codes', 'structures', 'urllib3', 'utils', 'warnings']
200
{"key":"\/apisix\/ssls\/1","value":{"type":"server","create_time":1672889225,"cert":"-----BEGIN CERTIFICATE-----\nMIIEKjCCApKgAwIBAgIQJ7+tTZRECIp1\/rZvizFsADANBgkqhkiG9w0BAQsFADBl\nMR4wHAYDVQQKExVta2NlcnQgZGV2ZWxvcG1lbnQgQ0ExHTAbBgNVBAsMFHJvb3RA\n\/8xj78nya58YZ6oaf+EDVqA+JPhbAZEFC+U9+BYLmr5eB31DNc\nNfeqlIybQ1xrj42QRUTGPIwfl6lwGu4L63WKVfSOOEWkx1yB\/x5vUYmWK4Vub+Gs\nrwqoluFp44oNMY+uy9w=\n-----END CERTIFICATE-----\n","id":"1","status":1,"key":"YnwfPfngS\/3wJFDtm8aCDkPmoYaHW5Qh60oD0cE2k8fEjtgy1JuHR+W2p5KI2dm5knsWwGRTTNwLPpglGsDSg2OcbVzkUNQEFMpZqGNMTDCMBhlpTZg=","snis":["lvh.me"],"update_time":1673334815}}

@spacewander
Copy link
Member

LGTM. So mtls.md also has the same problem. Would you also fix it in this PR?
Thanks!

…the file name, an error occurs.AttributeError: module 'requests' has no attribute 'put'
@xuruidong
Copy link
Contributor Author

LGTM. So mtls.md also has the same problem. Would you also fix it in this PR? Thanks!

The problem has been fixed

spacewander
spacewander previously approved these changes Jan 13, 2023
tokers
tokers previously approved these changes Feb 2, 2023
@xuruidong
Copy link
Contributor Author

Can this commit be merged ? Thanks ! @spacewander

@spacewander spacewander requested a review from pottekkat February 9, 2023 00:57
Copy link
Member

@pottekkat pottekkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xuruidong thank you for this PR. This looks good. You can also add a title to the code block to explicitly specify filename as we do in other documents like here: https://github.com/apache/apisix/blob/release/3.1/docs/en/latest/installation-guide.md?plain=1#L255

If you can add that title, you can omit the comment "save this file as ..." from each of these documentation pages.

This is also mentioned in our documentation style guide: https://apisix.apache.org/docs/general/documentation-style-guide/#formatting-punctuation-and-organization

Sorry for nitpicking, but consistency = quality. In the future, everyone will take it up themselves to follow the style guide.

@xuruidong xuruidong dismissed stale reviews from tokers and spacewander via dba1baa February 9, 2023 13:33
@xuruidong
Copy link
Contributor Author

@xuruidong thank you for this PR. This looks good. You can also add a title to the code block to explicitly specify filename as we do in other documents like here: https://github.com/apache/apisix/blob/release/3.1/docs/en/latest/installation-guide.md?plain=1#L255

If you can add that title, you can omit the comment "save this file as ..." from each of these documentation pages.

This is also mentioned in our documentation style guide: https://apisix.apache.org/docs/general/documentation-style-guide/#formatting-punctuation-and-organization

Sorry for nitpicking, but consistency = quality. In the future, everyone will take it up themselves to follow the style guide.

Code block titles are added. Please review again, thank you !

@spacewander spacewander merged commit 0bc65ea into apache:master Feb 12, 2023
@xuruidong xuruidong deleted the certificate_md_fix branch February 12, 2023 13:40
hongbinhsu added a commit to fitphp/apix that referenced this pull request Feb 13, 2023
* upstream/master:
  docs: change the file name to 'create-ssl.py'.If 'ssl.py' is used as … (apache#8623)
  feat: Body transformer plugin (apache#8766)
  fix: mocking plugin panic when response_example contain $ (apache#8810) (apache#8816)
  feat: file logger plugin support response body in variable (apache#8711)
  docs(getting-started): rewrite the install section (apache#8807)
  feat: allow each logger to define custom log format in its conf (apache#8806)
  fix(etcd): reloaded data may be in res.body.node (apache#8736)
  fix: fix fetch all service info from consul (apache#8651)
  docs: fix global-rule.md wrong curl  address (apache#8793)
  feat: stream subsystem support consul service discovery (apache#8696)
  chore(kafka-logger): support configuration `meta_refresh_interval` parameter (apache#8762)
  feat: ready to release 2.15.2 (apache#8783)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants