I've been trying out the 'socket' module in Python but whenever I attempt to run this code :
import socket
import sys
host = '192.168.1.1'
port = 23
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except:
print "socket() failed"
sys.exit(1)
Then it dies. Here is the error without the except and try :
Traceback (most recent call last):
File "C:\Documents and Settings\a\Desktop\socket.py", line 1, in <module>
import socket
File "C:\Documents and Settings\a\Desktop\socket.py", line 6, in <module>
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: 'module' object has no attribute 'AF_INET'
I've just started Python(Today) and am in need of help.
I saved it as socket.py but also sock.py ...
import socket
looks into the current directory before Python's standard library. And since your file is called socket.py, it is itself imported instead of the socket standard library module. Rename (don't copy, since that would leave the original file) the file to something else, such as my_socket.py.
Related
Not able to take ssh of a device using python getting below error.
Tried reinstalling python paramiko but didnt worked
import paramiko
import sys
import time
paramiko.client.SSHClient()
HOST = "192.168.1.11"
USER = "cisco"
PASS = "cisco"
client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client1.connect(HOST,username=USER,password=PASS)
print "SSH connection to %s established" %HOST
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/testssh.py", line 1, in
import paramiko
File "C:\Python27\lib\site-packages\paramiko__init__.py", line 31, in
from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy
File "C:\Python27\lib\site-packages\paramiko\client.py", line 24, in
import getpass
File "C:/Users/Administrator/Desktop\getpass.py", line 11, in
remote_conn_pre=paramiko.SSHClient()
AttributeError: 'module' object has no attribute 'SSHClient'
Change this
client1=paramiko.SSHClient()
to this
client1=paramiko.client.SSHClient()
Your best clue is the last line of the stack trace:
line 11, in remote_conn_pre=paramiko.SSHClient()
AttributeError: 'module' object has no attribute 'SSHClient'
(Added some whitespace for clarity)
Here it is saying that on this line, it can't find the property SSHClient in the paramiko class.
I would say to check the paramiko documentation, but you can see that you have successfully called this function on line 4:
paramiko.client.SSHClient()
Which will probably also work on line 11 - as other answers have pointed out, you need to access it through paramiko.client, not just paramiko. You probably also don't need line 4.
I have this python code which getting data from socket's and printing to console. But in working process I getting this error code
Traceback (most recent call last):
File "server.py", line 1, in <module>
import ssl, socket
File "/home/ssl.py", line 20, in <module>
returned from time.time())
AttributeError: 'module' object has no attribute 'wrap_socket'
maybe somebody has any idea how I can fix this issue?
import ssl, socket
sock = ssl.wrap_socket(socket.socket(), 'server.key', 'server.crt', True)
sock.bind( ('', 443) )
sock.listen(10)
while True:
conn, addr = sock.accept()
data = conn.recv(4)
print data
thank you
You most likely have a file named ssl.py in the same directory, as indicated by this part of the error:
File "/home/ssl.py", line 20, in
returned from time.time())
and the script is trying to import that instead of the system level module. If you cannot change the name of that file, try using absolute and relative imports.
I am trying to connect to some router using the above code and I am using juniperj2320.py module and in test file I have
am using code base from this http://subversion.assembla.com/svn/clauzsistel08/trunk/dist/
#!/usr/bin/python
from localconf import *
from juniperj2320 import *
import sys
import traceback
SERIALDEVICE = '/dev/ttyUSB0'
# Now configure the router
try:
router = JuniperJ2320(SERIALDEVICE)
except RouterConfigurationException, err:
print "Router configuration error: ", err
print "Please try again."
sys.exit(1)
but am getting this following error
./test.py
> /root/pyserial-2.6/examples/serialrouter.py(37)__init__()
-> serial.Serial.__init__(self, serialdevice, baudrate=baudrate, \
(Pdb) c
Traceback (most recent call last):
File "./test.py", line 30, in
router = JuniperJ2320(SERIALDEVICE)
File "/root/pyserial-2.6/examples/juniperj2320.py", line 32, in __init__
BYTESIZE, PARITY, STOPBITS, TIMEOUT)
File "/root/pyserial-2.6/examples/serialrouter.py", line 44, in __init__
fdpexpect.fdspawn.__init__(self, self.fileno())
File "/usr/lib/python2.6/site-packages/fdpexpect.py", line 40, in __init__
spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile )
File "/usr/lib/python2.6/site-packages/pexpect.py", line 412, in __init__
self.closed = True # File-like object.
AttributeError: can't set attribute
and absolutely clue less on wat happening here ! any help ll be greatly appreciated
thanks
This is a little bit of a shot in the dark, because I'm not familiar with the modules you're using, but based on the traceback, it looks like the constructor is expecting a file-like object, not just a file path. Try this instead.
SERIALDEVICE = '/dev/ttyUSB0'
# Now configure the router
try:
router = JuniperJ2320(open(SERIALDEVICE))
Instead of writing your own serial communications program wouldn't be easier to have pexpect drive a serial program like minicom?
I have problems when try to import pywhois module, i can import my own module but still have troubles when import pywhois. I moved the pywhois folder in my project folder domain, the screentshot is below:
This is the import and calling statment below:
import part:
from pywhois import *
calling part:
w = whois('hackbo.com')
self.response.out.write(w)
The problems is it will always throw a http 500 error, error text is below:
Traceback (most recent call last):
File "/home/li/Desktop/google_appengine/google/appengine/ext/webapp/__init__.py", line 700, in __call__
handler.get(*groups)
File "/home/li/Desktop/google_appengine/domain/main.py", line 43, in get
w = whois('hackbo.com')
File "pywhois/__init__.py", line 12, in whois
text = nic_client.whois_lookup(None, domain, 0)
File "pywhois/whois.py", line 157, in whois_lookup
result = self.whois(query_arg, nichost, flags)
File "pywhois/whois.py", line 100, in whois
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'socket'
What is this error means? why i could import my own module but can not import pywhois?
Google App Engine doesn't support the socket module, which pywhois uses. It allows you to import socket, but it's an empty module.
import whois
site = input ("Enter Site : ")
n = whois.whois (site)
print (n)
this is best whois
Enjoy that, easy.
I'm creating a very simple example on OSX with python 2.6 but I keep getting:
Traceback (most recent call last):
File "ssl.py", line 1, in <module>
import socket, ssl
File "/Users/Dennis/ssl.py", line 5, in <module>
sslSocket = ssl.wrap_socket(s)
AttributeError: 'module' object has no attribute 'wrap_socket'
Code:
import socket, ssl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('irc.freenode.net', 7000))
sslSocket = ssl.wrap_socket(s)
print repr(sslSocket.server())
print repr(sslSocket.issuer())
sslSocket.write('Hello secure socket\n')
s.close()
What am I doing terribly wrong?
Thanks!
Dennis
Don't name your script ssl.py, because when you name your script ssl.py and you do import ssl you're importing this same script .
Your script is : ssl.py
When you do an import ssl, it calls itself and that is why you get the AttributeError
Give another name to your script and it should work.