socket_dowanload test passed

This commit is contained in:
lyon1998 2024-04-24 21:12:59 +08:00
parent 612d353090
commit 7056f1bb01
6 changed files with 58 additions and 31 deletions

View File

@ -1,7 +1,18 @@
assert b"\r\n" in b"Hello\r\nWorld"
assert b"\r\n\r\n" in b"Hello\r\n\r\nWorld"
assert not (b"\r\n\r\n" in b"Hello\r\nWorld")
assert not (b"\r\n\r\n" in b'\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x39\x34\x31\x32\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x52\x61\x6e\x67\x65\x73\x3a\x20\x62\x79\x74\x65\x73\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20')
def test():
a = bytes(10)
a[1] = 1
return a
print(test())
res = test()

View File

@ -104,9 +104,10 @@ TEST_RUN_SINGLE_FILE(modbus, rtu_request, "test/python/modbus/rtu_request.py")
TEST_RUN_SINGLE_FILE(PikaStdDevice,
inhert,
"test/python/PikaStdDevice/inhert.py")
TEST_RUN_SINGLE_FILE(socket,
socket_download,
"test/python/socket/socket_download.py")
TEST_RUN_SINGLE_FILE_EXCEPT_OUTPUT(socket,
socket_download,
"test/python/socket/socket_download.py",
"PASS\r\n")
#endif

View File

@ -1,7 +1,18 @@
assert b"\r\n" in b"Hello\r\nWorld"
assert b"\r\n\r\n" in b"Hello\r\n\r\nWorld"
assert not (b"\r\n\r\n" in b"Hello\r\nWorld")
assert not (b"\r\n\r\n" in b'\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x63\x6c\x6f\x73\x65\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x39\x34\x31\x32\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x52\x61\x6e\x67\x65\x73\x3a\x20\x62\x79\x74\x65\x73\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20')
def test():
a = bytes(10)
a[1] = 1
return a
print(test())
res = test()

View File

@ -31,12 +31,18 @@ def http_download_file(url: str, file_path: str, buff_size=1024):
# Open file to write
f = open(file_path, 'wb') # Manually open the file
data_received = False
# try:
head_received = False
while True:
data = sock.recv(buff_size)
try:
data = sock.recv(buff_size)
except:
print('End of data')
break
print("[Data received:", len(data), ']')
sz = f.write(data)
print(data.decode())
if head_received:
sz = f.write(data)
# print("Data written:", sz)
# print(data.decode())
if len(data) == 0:
print("Length of data:", len(data))
if not data_received:
@ -48,16 +54,13 @@ def http_download_file(url: str, file_path: str, buff_size=1024):
data_received = True
# Handle the end of the HTTP header if it's still present
if b'\r\n\r\n' in data:
print(data.split(b'\r\n\r\n', 1))
header, data = data.split(b'\r\n\r\n')
print("Header:", header.decode())
print("Data:", data.decode())
# except:
# print("Error while receiving data")
# f.close()
# sock.close()
# return -1
if head_received == False:
if b'\r\n\r\n' in data:
# print("Header received", data)
head_received = True
aplited = data.split(b'\r\n\r\n', 1)
if len(aplited) == 2:
sz = f.write(aplited[1])
# Close file and socket manually
f.close()
@ -66,4 +69,5 @@ def http_download_file(url: str, file_path: str, buff_size=1024):
return 0
http_download_file("http://pikapython.com", "pikapython.html")
assert http_download_file("http://pikapython.com", "pikapython.html") == 0
print("PASS")

View File

@ -4334,28 +4334,28 @@ int builtins_bytearray___getitem__(PikaObj* self, int __key) {
pika_bool _bytes_contains(Arg* self, Arg* others) {
ArgType type = arg_getType(others);
if (type == ARG_TYPE_BYTES) {
if (arg_getBytesSize(self) > arg_getBytesSize(others)) {
return pika_false;
}
uint8_t* bytes1 = arg_getBytes(self);
uint8_t* bytes2 = arg_getBytes(others);
size_t size1 = arg_getBytesSize(self);
size_t size2 = arg_getBytesSize(others);
size_t i = 0;
size_t j = 0;
while (i < size1 && j < size2) {
if (size1 > size2) {
return pika_false;
}
size_t i = 0, j = 0, start_j = 0;
while (j < size2) {
if (bytes1[i] == bytes2[j]) {
i++;
j++;
if (i == size1) {
return pika_true;
}
} else {
j++;
j = ++start_j; // Move `j` to the next start position
i = 0; // Reset `i`
}
}
if (i == size1) {
return pika_true;
} else {
return pika_false;
}
}
return pika_false;
}

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 13
#define PIKA_VERSION_MICRO 3
#define PIKA_EDIT_TIME "2024/04/24 20:40:16"
#define PIKA_EDIT_TIME "2024/04/24 21:12:52"