正则表达式:(或不)只在头之后打印数据

2024-04-26 20:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

一些家庭作业的帮助将不胜感激。你知道吗

使用socket,我需要解析来自网站(http://www.py4inf.com/code/romeo.txt)的数据。你知道吗

我使用正则表达式“^\s*$”来定位头后面和数据上方的第一个空行。你知道吗

关于如何只提取数据(而不打印标题)有什么提示吗?你知道吗

import socket
import re

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    userUrl = raw_input('Enter a url: ')

    d = userUrl.split('/')
    d.remove("")

    host = d[1]

    mysock.connect((host, 80))
    mysock.send('GET %s HTTP/1.0\n\n'%(userUrl))


    while True:
        data = mysock.recv(3000)
        if len(data) < 1: break
        print (''.join([x for x in re.findall(**'^\s*$'**,data,re.DOTALL)]))           


except Exception as e:
    print (str(e))

Tags: 数据importrecomhttphostdata网站
2条回答

我假设,既然这是一个家庭作业问题,你就必须使用socket,不能使用像^{}这样更友好的东西。你知道吗

我将首先循环,直到您在字符串中得到完整的响应,然后像这样迭代:

...
response = ""
while True:
    data = mysock.recv(3000)
    if len(data) < 1: break
    response += data

iterator = iter(response.split("\n"))

for line in iterator:
    if not line.strip():  # empty line
        break

body = "\n".join(iterator)  # put the rest of the data in a string

>> Documentation of ^{} (Python 3)

首先,建议使用2字节的幂作为socket.recv的缓冲区大小:

data = mysock.recv(4096)

其次,它不返回字符串,而是返回长度为bufsize字节的二进制数据(如果到达流结尾,则返回长度小于或等于)。这意味着,您不能逐行获取数据,而可以像遍历类似文件的对象一样对其进行迭代。你知道吗

您必须收集数据块,连接它们,将结果转换为字符串,然后将其拆分为行列表。下面是一个从套接字流返回行的生成器函数,以便您可以像遍历类似文件的对象一样遍历它:

更新:修复了以下函数中的字节编码问题

def read_lines_from_socket(mysock):
    data = ""
    while True:
        received = mysock.recv(64)
        if len(received) < 1: 
            return data
        data += received.decode("utf-8")
        if "\n" in data:
            lines = data.split("\n")
            data = lines.pop()  # move not yet completed line back to beginning of input data
            for line in lines:
                yield line

注意,这个函数已经从返回的行中去掉了换行符\n

现在,您可以像使用文件一样使用此函数返回的生成器,并在收到的行上进行迭代:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    userUrl = 'http://www.py4inf.com/code/romeo.txt'

    d = userUrl.split('/')
    d.remove("")

    host = d[1]

    mysock.connect((host, 80))
    mysock.send('GET %s HTTP/1.0\n\n'%(userUrl))

    ### vvvvv  New example code section starts here:  vvvvv ###    

    header_data = True
    for line in read_lines_from_socket(mysock):
        if header_data:
            if not line.strip():
                # checks for first empty line and sets header_data to False after that
                header_data = False
                print("  - End Of Header   -")

            else:
                # process header data here:
                print("Header:", line)

        else:

            # process content data here:
            print("Content:", line)

    ### ^^^^^  New example code section ends here.  ^^^^^ ###    


except Exception as e:
    print (str(e))

相关问题 更多 >