motivation
- Some bugs may not reproduce locally due to inconsistencies between local and online environments
- Inconsistent versions of local and online dependencies can also cause problems
- Sometimes there are bugs related to data, where local data doesn’t match online data
- Some third-party platforms will verify the validity of the server or asynchronous callback results, such as wechat Pay, which cannot be tested locally
As mentioned above, wouldn’t it be nice to have an easy way to debug a remote server? PyCharm makes it easy to implement remote debugging. Here’s a look at PyCharm’s awesome feature.
Adding remote Deployment
- Open PyCharm, Tools –>Deployment–>Configuration
- Click the “+” on the left to add a remote server, give it any name, and select SFTP
- Configure the connection
-
Configure the Mappings
-
Configuring Excluded Paths (Optional)
Finally, click OK
If you open the deployment option again, you can now deploy the code directly to the server, or download the code directly to the local server. Automatic Upload is checked by default, and I usually remove it to prevent some local test code from being uploaded
Add a new file, look at deployment options, and you can upload it to the remote server
Add a remote interpreter
Remote deployment is all about synchronizing and copying files; to truly implement remote debugging, you need to configure the remote interpreter
- Setup page go to “Project Interpreter” –> Settings –>Add
- Select “SSH Interpreter” –> “Existing Server Configuration “–> select the deployment configuration you just created. Use either” Create “or” Move”
- Click Next to connect to the remote server. After a while, the following screen will appear. After a little configuration, click “Finish”
Finally, use the remote interpreter and click “OK” to return to the project interface and wait for the synchronization to complete
Remote debugging
Here is an example of Flask+ Ari Cloud +nginx+ running a script
- Add a Run Configuration
- Run the script as follows, where port 8000 is used
Set Host to 0.0.0.0
- Nginx is configured with port 8000
server { listen 8000 ; listen [::]:8000 ; server_name _; root /usr/share/nginx/html; access_log /var/log/nginx/access_8000.log main; error_log /var/log/nginx/error_8000.log; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; Location / {proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }Copy the code
- If ali cloud is used, the security group needs to open port 8000
- Breakpoint run
- When the image above appears, congratulations, you can debug the remote server with breakpoints
We hit a breakpoint, then try to access an API service: xx, xx, xx, xx: 8000 / API/pages/b…
On the perfect break
added
This feature is not recommended on the formal server, but can be used on the test server