我是Python和网络的新手,我知道我的问题可能听起来多余,但我花了几个小时研究from socket import *
和import socket
之间的区别以及不同类型的套接字对象。我目前正在制作一个简单的数据包嗅探器,并且已经收到很多属性错误。我注意到如果我写import socket
并执行dir(socket)
,我会得到:
AF_APPLETALK
AF_DECnet
AF_INET
AF_INET6
AF_IPX
AF_LINK
AF_ROUTE
AF_SNA
AF_SYSTEM
AF_UNIX
AF_UNSPEC
AI_ADDRCONFIG
AI_ALL
AI_CANONNAME
AI_DEFAULT
AI_MASK
AI_NUMERICHOST
AI_NUMERICSERV
AI_PASSIVE
AI_V4MAPPED
AI_V4MAPPED_CFG
AddressFamily
CAPI
CMSG_LEN
CMSG_SPACE
EAGAIN
EAI_ADDRFAMILY
EAI_AGAIN
EAI_BADFLAGS
EAI_BADHINTS
EAI_FAIL
EAI_FAMILY
EAI_MAX
EAI_MEMORY
EAI_NODATA
EAI_NONAME
EAI_OVERFLOW
EAI_PROTOCOL
EAI_SERVICE
EAI_SOCKTYPE
EAI_SYSTEM
EBADF
EWOULDBLOCK
INADDR_ALLHOSTS_GROUP
INADDR_ANY
INADDR_BROADCAST
INADDR_LOOPBACK
INADDR_MAX_LOCAL_GROUP
INADDR_NONE
INADDR_UNSPEC_GROUP
IPPORT_RESERVED
IPPORT_USERRESERVED
IPPROTO_AH
IPPROTO_DSTOPTS
IPPROTO_EGP
IPPROTO_EON
IPPROTO_ESP
IPPROTO_FRAGMENT
IPPROTO_GGP
IPPROTO_GRE
IPPROTO_HELLO
IPPROTO_HOPOPTS
IPPROTO_ICMP
IPPROTO_ICMPV6
IPPROTO_IDP
IPPROTO_IGMP
IPPROTO_IP
IPPROTO_IPCOMP
IPPROTO_IPIP
IPPROTO_IPV4
IPPROTO_IPV6
IPPROTO_MAX
IPPROTO_ND
IPPROTO_NONE
IPPROTO_PIM
IPPROTO_PUP
IPPROTO_RAW
IPPROTO_ROUTING
IPPROTO_RSVP
IPPROTO_TCP
IPPROTO_TP
IPPROTO_UDP
IPPROTO_XTP
IPV6_CHECKSUM
IPV6_DSTOPTS
IPV6_HOPLIMIT
IPV6_HOPOPTS
IPV6_JOIN_GROUP
IPV6_LEAVE_GROUP
IPV6_MULTICAST_HOPS
IPV6_MULTICAST_IF
IPV6_MULTICAST_LOOP
IPV6_NEXTHOP
IPV6_PKTINFO
IPV6_RECVTCLASS
IPV6_RTHDR
IPV6_RTHDR_TYPE_0
IPV6_TCLASS
IPV6_UNICAST_HOPS
IPV6_V6ONLY
IP_ADD_MEMBERSHIP
IP_DEFAULT_MULTICAST_LOOP
IP_DEFAULT_MULTICAST_TTL
IP_DROP_MEMBERSHIP
IP_HDRINCL
IP_MAX_MEMBERSHIPS
IP_MULTICAST_IF
IP_MULTICAST_LOOP
IP_MULTICAST_TTL
IP_OPTIONS
IP_RECVDSTADDR
IP_RECVOPTS
IP_RECVRETOPTS
IP_RETOPTS
IP_TOS
IP_TTL
IntEnum
LOCAL_PEERCRED
MSG_CTRUNC
MSG_DONTROUTE
MSG_DONTWAIT
MSG_EOF
MSG_EOR
MSG_OOB
MSG_PEEK
MSG_TRUNC
MSG_WAITALL
NI_DGRAM
NI_MAXHOST
NI_MAXSERV
NI_NAMEREQD
NI_NOFQDN
NI_NUMERICHOST
NI_NUMERICSERV
PF_SYSTEM
SCM_CREDS
SCM_RIGHTS
SHUT_RD
SHUT_RDWR
SHUT_WR
SOCK_DGRAM
SOCK_RAW
SOCK_RDM
SOCK_SEQPACKET
SOCK_STREAM
SOL_IP
SOL_SOCKET
SOL_TCP
SOL_UDP
SOMAXCONN
SO_ACCEPTCONN
SO_BROADCAST
SO_DEBUG
SO_DONTROUTE
SO_ERROR
SO_KEEPALIVE
SO_LINGER
SO_OOBINLINE
SO_RCVBUF
SO_RCVLOWAT
SO_RCVTIMEO
SO_REUSEADDR
SO_REUSEPORT
SO_SNDBUF
SO_SNDLOWAT
SO_SNDTIMEO
SO_TYPE
SO_USELOOPBACK
SYSPROTO_CONTROL
SocketIO
SocketKind
SocketType
TCP_MAXSEG
TCP_NODELAY
_GLOBAL_DEFAULT_TIMEOUT
_GiveupOnSendfile
_LOCALHOST
_LOCALHOST_V6
__all__
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__spec__
_blocking_errnos
_intenum_converter
_realsocket
_socket
create_connection
dup
errno
error
fromfd
gaierror
getaddrinfo
getdefaulttimeout
getfqdn
gethostbyaddr
gethostbyname
gethostbyname_ex
gethostname
getnameinfo
getprotobyname
getservbyname
getservbyport
has_ipv6
herror
htonl
htons
if_indextoname
if_nameindex
if_nametoindex
inet_aton
inet_ntoa
inet_ntop
inet_pton
io
ntohl
ntohs
os
selectors
setdefaulttimeout
sethostname
socket
socketpair
sys
timeout
Process finished with exit code 0
现在,如果我将其更改为from socket import *
,我会:
__class__
__delattr__
__dir__
__doc__
__enter__
__eq__
__exit__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__slots__
__str__
__subclasshook__
__weakref__
_accept
_check_sendfile_params
_closed
_decref_socketios
_io_refs
_real_close
_sendfile_use_send
_sendfile_use_sendfile
accept
bind
close
connect
connect_ex
detach
dup
family
fileno
get_inheritable
getpeername
getsockname
getsockopt
gettimeout
listen
makefile
proto
recv
recv_into
recvfrom
recvfrom_into
recvmsg
recvmsg_into
send
sendall
sendfile
sendmsg
sendto
set_inheritable
setblocking
setsockopt
settimeout
shutdown
timeout
type
Process finished with exit code 0
我不明白。他们为什么如此不同?当我以一种方式导入它时,有一种东西没有属性,当我以另一种方式导入它时,另一种东西没有属性。似乎一种类型的导入访问套接字库中的所有常量,而另一种方式将访问方法。我觉得这与可以制作的不同类型的套接字对象有关,但我不太确定。我会发布我的代码,以防我写的东西出现问题:
import socket
import os
class Sniff:
def __init__(self, HOST):
self.HOST = HOST
self.sniffer = socket
def sniff_packet(self, host, win, socket_prot):
self.sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_prot)
self.sniffer.bind((host, 80))
self.sniffer.setsocketopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
if win == 1:
self.sniffer.iotcl(socket.SIO_RCVALL, socket.RCVALL_ON)
print(self.sniffer.recvfrom(65565))
def main(self):
if os.name == 'nt':
self.sniff_packet(self.HOST, 1, socket.IPPROTO_IP)
else:
self.sniff_packet(self.HOST, 0, socket.IPPROTO_IP)
sniffObj = Sniff(input("Enter your network address: "))
sniffObj.main()
以下是运行此代码时出现的一些错误,具体取决于我导入套接字的方式:
1)import socket
:
Traceback (most recent call last):
File "PacketSniffer.py", line 28, in <module>
sniffObj.main()
File "PacketSniffer.py", line 25, in main
self.sniff_packet(self.HOST, 0, socket.IPPROTO_IP)
File "PacketSniffer.py", line 13, in sniff_packet
self.sniffer.setsocketopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
AttributeError: 'socket' object has no attribute 'setsocketopt'
2)from socket import *
Traceback (most recent call last):
File "PacketSniffer.py", line 28, in <module>
sniffObj.main()
File "PacketSniffer.py", line 25, in main
self.sniff_packet(self.HOST, 0, socket.IPPROTO_IP)
AttributeError: type object 'socket' has no attribute 'IPPROTO_IP'
请帮帮我!我想更好地理解套接字以及它们是如何工作的,我希望我的问题可以帮助那些和我一样陷入困境的人。
答案 0 :(得分:2)
为什么他们如此不同?
dir(socket)
的输出不同,因为socket
在两种情况下不是同一个对象。
>>> import socket
>>> type(socket)
<class 'module'>
>>> type(socket.socket)
<class 'type'>
VS
>>> from socket import *
>>> type(socket)
<class 'type'>
import socket
未在当前符号表中输入socket
模块的定义;它只进入模块socket
。使用前导模块名称,您可以访问定义,例如: G。 socket.socket
(尾部socket
是套接字对象的类型)。
from socket import *
输入所有socket
模块的定义,而不是以当前符号表中的下划线开头,因此,socket
只是套接字对象的类型。