简体   繁体   English

pycharm 中的 400 错误请求问题

[英]400 Bad Request issue in pycharm

When I am running following code in pycharm:当我在 pycharm 中运行以下代码时:

import urllib.request
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
    print(line.decode().strip())

I am getting and error我得到和错误

HTTP/1.1 400 Bad Request
Date: Fri, 03 Jul 2020 10:14:04 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 308
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at do1.dr-chuck.com Port 80</address>
</body></html>
Process finished with exit code 0**

While it works fine when I run the code in terminal/command promt.虽然当我在终端/命令提示符中运行代码时它工作正常。

It appears your Terminal and Pycharm are not running the code using the same python version, ie one is running Py2 and the other Py3.看来您的终端和 Pycharm 没有使用相同的 python 版本运行代码,即一个正在运行 Py2,另一个正在运行 Py3。

In Pycharm, you can set the desired version like so在 Pycharm 中,您可以像这样设置所需的版本

在此处输入图像描述

In your terminal verify the version, run -V for example if you use python [path] then python -V .在您的终端中验证版本,运行-V例如,如果您使用python [path]然后python -V

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM