admin管理员组文章数量:1122832
I am trying to create my own http server without using any low-level builtin library except ctypes but things are going terribly wrong.
My configuration is correct: IP address is correct, port is available, I have root privileges, using AF_INET
(IPV4 connection) tcp_protocol
with sock stream.
My config file includes:
{
"ipv4_connection" : 2,
"available_port" : 8081,
"host_address" : [127, 0, 0, 1],
"sock_stream" : 1,
"tcp_protocol" : 0
}
I have created a ctype socket object using ctypes.Structure
and everything looks good:
class SocketDeclaration(ctypes.Structure):
"""
-- load this instance when you have to bind the socket to the given address !
"""
_fields_ = [
('sin_family', ctypes.c_short),
('sin_port', ctypes.c_ushort),
('sin_addr', ctypes.c_ubyte * 4)
]
def __init__(self):
super().__init__()
self.config = ConfigurationFetcher()
self.sin_family = ctypes.c_short(self.config.ipv4_connection)
self.sin_port = ctypes.c_ushort((self.config.available_port >> 8) | (self.config.available_port << 8))
self.sin_addr = (ctypes.c_ubyte * 4)(*self.config.host_address)
The issue here is that despite getting the wrong answer from this call of bind
result = self.libc_object.bind(socket_object, instance_reference, instance_size)
from a method to handle errors gracefully being called as:
# Binding Socket to the Address
socket_instance = SocketDeclaration()
_ = self.bind_socket(new_socket, ctypes.byref(socket_instance), ctypes.sizeof(socket_instance))
it doesn't set the errorno
value to -1. My exception object says "unable to bind ; Code = success" - why?
For further clarification the address sin_address
is a reference to the object and not values, socket file descriptor returns 3 which is a valid response. I know syntax and just unable to figure about what is wrong.
strace
shows that I have some kind of invalid argument as sin_addr
but after reading a lot of documentation I just end up asking for the solution from any expert.
Any kind of help would be appreciated !
I literally tried everything; from changing ports to changing address! My machine is Kali kernal 6.10.11-amd64. I studied documentation from 5 sources including Python documentation, IBM, Stackoverflow, Chatgpt, Meta ai and it doesn't work!
本文标签: httpPython ctypes libc bind method Wrong AnswerStack Overflow
版权声明:本文标题:http - Python c_types libc bind method Wrong Answer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307659a1933389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论