Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将HTTP请求行发送到jupyter_Python_Python 3.x_Http_Jupyter Notebook - Fatal编程技术网

使用python将HTTP请求行发送到jupyter

使用python将HTTP请求行发送到jupyter,python,python-3.x,http,jupyter-notebook,Python,Python 3.x,Http,Jupyter Notebook,我试图在如下模块的帮助下向我的jupyter笔记本发送HTTP请求: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect(('127.0.0.1', 8888)) s.sendall(b'GET /api/contents HTTP/1.1 \n\n') # Maybe i didn't understand how HTTP requests work print(s.recv(1

我试图在如下模块的帮助下向我的jupyter笔记本发送HTTP请求:

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect(('127.0.0.1', 8888))
    s.sendall(b'GET /api/contents HTTP/1.1 \n\n') # Maybe i didn't understand how HTTP requests work
    print(s.recv(1024))
输出(在jupyter笔记本运行的终端中):

收到的数据是HTTP/1.1400错误请求\r\n\r\n

它说它使用这个分隔符
re.compile(b'\r?\n\r?\n')


如果您不想在
HTTP
请求中提供任何头,只需按
\r\n\r\n
顺序终止它即可:

s.sendall(b'GET /api/contents HTTP/1.1\r\n\r\n')
HTTP
使用
\r\n
序列作为行分隔符(如在Windows中)和双序列(
\r\n\r\n
)来标记请求头的结束。所以通常请求看起来像

GET /api/contents HTTP/1.1\r\n
User-Agent: blablah\r\n
....\r\n
\r\n
<HERE GOES REQUEST BODY>
GET/api/contents HTTP/1.1\r\n
用户代理:blablah\r\n
....\r\n
\r\n
GET /api/contents HTTP/1.1\r\n
User-Agent: blablah\r\n
....\r\n
\r\n
<HERE GOES REQUEST BODY>