introduction
During Python development, developers often encounter various errors and exceptions. Some errors are obvious, such as syntax errors; while others are more concealed, such as network connectivity issues or restrictions on third-party APIs. This article will summarize two typical Python development problems: SMTP server connection failure and f-string string processing errors, analyze its causes, and provide detailed solutions. The article will also include code samples and best practice suggestions to help developers avoid similar issues.
1. SMTP server connection failure problem
1.1 Error phenomenon
When trying to connect to the QQ mailbox SMTP server using Python's smtplib, the following error occurs:
2025-05-15 23:34:58,808 - app - ERROR - SMTP server connection failed: (-1, b'\x00\x00\x00')
Traceback (most recent call last):
File "/doudian-phone-tool/doudian/send_qq_email.py", line 70, in send_email_with_attachment
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
File "/usr/local/lib/python3.11/", line 284, in __exit__
raise SMTPResponseException(code, message)
: (-1, b'\x00\x00\x00')
1.2 Possible Causes
Service not enabled: QQ mailbox has SMTP service turned off by default and needs to be enabled manually.
2. Incorrect server configuration:
Incorrect SMTP server address (such as misspelling)
Incorrect port number (465 or 587)
3. Authorization issues:
Email password instead of authorization code (QQ mailbox requires authorization code instead of original password)
The authorization code has expired (the authorization code will be invalid after modifying the QQ email password)
4. Network or firewall restrictions:
The server cannot access SMTP service (such as the cloud server does not open port 465)
Local firewall or security group rules block connections
1.3 Solution
(1) Check and enable the QQ mailbox SMTP service
Log in to the QQ Email Web Edition → Enter Settings → Account
Find POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service
Turn on the IMAP/SMTP service and generate a new authorization code (16-bit string)
(2) Verify SMTP configuration
Make sure the SMTP configuration in the code is correct:
smtp_server = "" # Must be correctsmtp_port = 465 # SSL port, or 587 (STARTTLS)sender_email = "your_email@" password = "your_authorization_code" # Not the QQ email password!
(3) Improve error handling
Optimize the code, add more detailed error logs and retry mechanisms:
import smtplib import time from socket import gaierror def send_email_with_retry(sender, password, recipients, msg, max_retries=3): for attempt in range(max_retries): try: with smtplib.SMTP_SSL("", 465) as server: (sender, password) (sender, recipients, msg.as_string()) return True except : print("SMTP authentication failed: Please check the email and authorization code") return False except (, gaierror) as e: print(f"SMTPConnection failed(try {attempt + 1}/{max_retries}):{str(e)}") (2) # Retry after delaying by 2 seconds return False
2. Error in nesting quotation marks of f-string strings
2.1 Error phenomenon
When using f-string in Python code, syntax errors are caused by quotation nesting errors:
current_app.(f"【{item['waybillNum']}】way bill number, [{record["Mobile phone number"]}] mobile phone number is processed successfully, save the order to the database")
^^^^^^^^^
SyntaxError: f-string: unmatched '['
2.2 Cause analysis
Quotation conflict: The outer layer of f-string uses double quotes "...", and the internal dictionary key also uses double quotes record["Mobile Number"], causing the Python parser to fail to correctly recognize string boundaries.
Parse rules for f-string: Python's f-string requires that quotes must be paired correctly, otherwise a SyntaxError will be thrown.
2.3 Solution
(1) Use single quotes on the outer layer and double quotes on the inner layer
current_app.(f'【{item["waybillNum"]}】Way bill number,【{record["Phone number"]}】The mobile phone number is successfully processed,Save orders to database')
(2) Use single quotes in a uniform manner
current_app.(f'【{item[\'waybillNum\']}】Way bill number,【{record[\'Phone number\']}】The mobile phone number is successfully processed,Save orders to database')
(3) Use escape characters
current_app.(f"【{item['waybillNum']}】Way bill number,【{record['Phone number']}】The mobile phone number is successfully processed,Save orders to database")
2.4 Best Practices
Single quotes are preferred as string separators (recommended by the Python community).
Avoid mixing quotes, such as outer double quotes + inner double quotes.
Use formatting tools such as black or autopep8 to automatically adjust the code style.
3. Summary and Suggestions
3.1 How to avoid similar errors
Question Type | Preventive measures |
---|---|
SMTP connection failed | 1. Check whether the SMTP service is enabled 2. Use the correct authorization code 3. Add error retry mechanism |
F-string quotation mark error | 1. Uniform quote style 2. Use the code formatting tool 3. Avoid multi-layer nesting |
3.2 Debugging skills
Logging: Use the logging module to record detailed error information.
Step by step test: first test the SMTP connection (such as telnet 465), and then debug the code.
Static checking: Use pylint or flake8 to check for syntax problems.
This is the article about SMTP connection and string processing errors and solutions for Python development. This is all about this. For more common Python error resolution content, please search for my previous articles or continue browsing the related articles below. I hope you can support me in the future!