为什么我在不同的 python 版本上得到不同的输出 运行 相同的代码?
Why do I get different output running the same code on different python versions?
所以我一直在学习ctypes库,没想到结果。
我有以下代码只是为了测试 ctypes 是否正常工作:
from ctypes import *
libc = CDLL("libc.so.6")
message_string = "Hello world!\n"
libc.printf("Testing: %s", message_string)
我 运行 它与 python2.7 和 python3,虽然版本不同,但看起来应该有相同的结果。
root@Crushli-pc ~# python3 --version && python --version
Python 3.7.5
Python 2.7.17
root@Crushli-pc ~# python chapter1-printf.py
Testing: Hello world!
root@Crushli-pc ~# python3 chapter1-printf.py
T⏎ root@Crushli-pc ~#
我 知道 python2 支持在今年年初就结束了,这就是为什么我很好奇为什么 python3 没有按预期工作。
我已经运行编码的机器:
.............. root@Crushli-pc
..,;:ccc,. ---------------
......''';lxO. OS: Kali GNU/Linux Roll
.....''''..........,:ld; Kernel: 5.3.0-kali2-amd
.';;;:::;,,.x, Uptime: 1 hour, 21 mins
..'''. 0Xxoc:,. ... Packages: 2226 (dpkg)
.... ,ONkc;,;cokOdc',. Shell: bash 5.0.11
. OMo ':ddo. Resolution: 1080x1920,
dMc :OO; DE: Xfce
0M. .:o. WM: Xfwm4
;Wd WM Theme: Kali-Dark
;XO, Theme: Kali-Dark [GTK2/
,d0Odlc;,.. Icons: Flat-Remix-Blue-
..',;:cdOOd::,. Terminal: qterminal
.:d;.':;. Terminal Font: Fira Cod
'd, .' CPU: Intel i5-3570 (4)
;l .. GPU: NVIDIA GeForce GTX
.o Memory: 1791MiB / 7922M
c
.'
.
我会提供任何需要的信息,请问一下。
我的问题是我做错了什么以及为什么它现在不能正常工作。
编辑:
试过了 --> Differences in ctypes between Python 2 and 3,得到了这样的代码:
from ctypes import *
msvcrt = CDLL("libc.so.6")
message_string = str("Hello world!\n").encode('ascii')
msvcrt.printf("Testing: %s", message_string)
仍然,我得到了相同的结果:
root@Crushli-pc ~# cat chapter1-printf.py
from ctypes import *
msvcrt = CDLL("libc.so.6")
message_string = str("Hello world!\n").encode('ascii')
msvcrt.printf("Testing: %s", message_string)
root@Crushli-pc ~# python3 chapter1-printf.py
T⏎ root@Crushli-pc ~# python chapter1-printf.py
Testing: Hello world!
root@Crushli-pc ~#
由于 printf
需要字节字符串,因此您应该在传递字符串之前对其进行编码:
libc.printf("Testing with non-ascii chars abc€ éю: %s".encode('utf8'),
message_string.encode('utf8'))
输出:
Testing with non-ascii chars abc€ éю: Hello world!
所以我一直在学习ctypes库,没想到结果。
我有以下代码只是为了测试 ctypes 是否正常工作:
from ctypes import *
libc = CDLL("libc.so.6")
message_string = "Hello world!\n"
libc.printf("Testing: %s", message_string)
我 运行 它与 python2.7 和 python3,虽然版本不同,但看起来应该有相同的结果。
root@Crushli-pc ~# python3 --version && python --version
Python 3.7.5
Python 2.7.17
root@Crushli-pc ~# python chapter1-printf.py
Testing: Hello world!
root@Crushli-pc ~# python3 chapter1-printf.py
T⏎ root@Crushli-pc ~#
我 知道 python2 支持在今年年初就结束了,这就是为什么我很好奇为什么 python3 没有按预期工作。
我已经运行编码的机器:
.............. root@Crushli-pc
..,;:ccc,. ---------------
......''';lxO. OS: Kali GNU/Linux Roll
.....''''..........,:ld; Kernel: 5.3.0-kali2-amd
.';;;:::;,,.x, Uptime: 1 hour, 21 mins
..'''. 0Xxoc:,. ... Packages: 2226 (dpkg)
.... ,ONkc;,;cokOdc',. Shell: bash 5.0.11
. OMo ':ddo. Resolution: 1080x1920,
dMc :OO; DE: Xfce
0M. .:o. WM: Xfwm4
;Wd WM Theme: Kali-Dark
;XO, Theme: Kali-Dark [GTK2/
,d0Odlc;,.. Icons: Flat-Remix-Blue-
..',;:cdOOd::,. Terminal: qterminal
.:d;.':;. Terminal Font: Fira Cod
'd, .' CPU: Intel i5-3570 (4)
;l .. GPU: NVIDIA GeForce GTX
.o Memory: 1791MiB / 7922M
c
.'
.
我会提供任何需要的信息,请问一下。
我的问题是我做错了什么以及为什么它现在不能正常工作。
编辑: 试过了 --> Differences in ctypes between Python 2 and 3,得到了这样的代码:
from ctypes import *
msvcrt = CDLL("libc.so.6")
message_string = str("Hello world!\n").encode('ascii')
msvcrt.printf("Testing: %s", message_string)
仍然,我得到了相同的结果:
root@Crushli-pc ~# cat chapter1-printf.py
from ctypes import *
msvcrt = CDLL("libc.so.6")
message_string = str("Hello world!\n").encode('ascii')
msvcrt.printf("Testing: %s", message_string)
root@Crushli-pc ~# python3 chapter1-printf.py
T⏎ root@Crushli-pc ~# python chapter1-printf.py
Testing: Hello world!
root@Crushli-pc ~#
由于 printf
需要字节字符串,因此您应该在传递字符串之前对其进行编码:
libc.printf("Testing with non-ascii chars abc€ éю: %s".encode('utf8'),
message_string.encode('utf8'))
输出:
Testing with non-ascii chars abc€ éю: Hello world!