I'm getting an attribute error from running the client side of the program, I'm pretty sure I did it everything correctly but apparently not.
Here's the code:
from socket import *
serverName = 'hostname'
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
This is the error I get:
Traceback (most recent call last):
File "UDPClient.py", line 4, in <module>
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: type object '_socketobject' has no attribute 'socket'
EDIT:
Traceback (most recent call last):
File "UDPClient.py", line 6, in <module>
clientSocket.sendto(message,(serverName,serverPort))
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
You're getting this wrong. Since you import *, just use AF_INET and SOCK_DGRAM
>>> from socket import *
>>> clientSocket = socket(AF_INET, SOCK_DGRAM)
Tested on my machine using Py3.4
Related
I have this code:
socks = []
for i in range(20):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(False)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 0))
socks.append(s)
# ...... Some code triggering s.connect_ex to calculate connection time ......
for s in socks:
s.shutdown(socket.SHUT_RDWR)
s.close()
Sometimes this code produces the following exception:
2022-07-04 16:06:59,147 ERROR - Thread 'Thread-239' failed
Traceback (most recent call last):
File "/root/connection_time/lib/time_socks.py", line 279, in time_socks
s.bind(('0.0.0.0', 0))
OSError: [Errno 98] Address already in use
Ps. The code in running as a root user.
I'm trying to use the socket object in one of my python codes and it fails at this line:
#!/usr/bin/python
import subprocess,socket
HOST = '127.0.0.1'
PORT = '443'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Yo')
while 1:
data = s.recv(1024)
if data == "quit": break
proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
stdoutput1 = proc.stdout.read() + proc.stderr.read()
s.send(stdoutput)
s.send('Bye')
s.close()
It fails at: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: an integer is required
It's telling me an integer is required in the parameters when I can't see why it would be. It calls this method from socket.py:
def meth(name,self,*args):
return getattr(self._sock,name)(*args)
You have misunderstood the error message, and are looking at the wrong line. It is the s.connect() call that fails:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('127.0.0.1', '443'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
TypeError: an integer is required
Note that it is not the socket.socket() call that failed here, it is the s.connect() call instead, because '443' is not a valid port number.
Port numbers must be integers, not strings; correct your PORT variable:
PORT = 443 # make this an integer
With that change connecting works (provided the port is connectable):
>>> s.connect(('127.0.0.1', 443))
Hello I am new to python , I am writing a code which generates dns requests
from socket import error as socket_error
import threading
from random import randint
from time import sleep
def task(number):
try :
HOST = Random_website.random_website().rstrip() # fetches url
PORT = 80 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print(str(number) +":"+HOST +"Connected")
except socket_error as serr:
if serr.errno != errno.ECONNREFUSED:
# Not the error we are looking for, re-raise
raise serr
thread_list = []
for i in range(1, 100):
t = threading.Thread(target=task, args=(i,))
thread_list.append(t)
for thread in thread_list:
thread.start()
Executing above code throws this error, can anyone help me out of this
I am pulling out my hair from one day
Thanks in advance
Like this:
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(("www.google.com",80))
>>> s.connect(("http://www.google.com",80))
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
s.connect(("http://www.google.com",80))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
gaierror: [Errno -2] Name or service not known
>>>
Socket not a HTTP connection !
Remove HTTP:// tag before sending a request !
EDIT:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(("digan.net",80))
>>> s.connect(("digan.net/hahaha/hihihi/etc",80))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
s.connect(("digan.net/hahaha/hihihi/etc",80))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
gaierror: [Errno -2] Name or service not known
>>>
Socket can't send request to additional path. Only talk with server !
I got the same error message when a web proxy was "on", but the url pointed to a machine in the local network accessible without proxy. Setting the web-proxy (http) to "off" fixed it for me.
Hope this helps, Alex.
I can't define a server with the real IP address of my computer, it gives me an error.When I do this with my local IP address it works but I want to run the client in other computer
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('my_ip',9876))
server_socket.listen(1)
(client_socket,client_address)=server_socket.accept()
recived_data=client_socket.recv(999999999)
password=open(r'D:\passwords','wb')
password.write(recived_data)
client_socket.close()
server_socket.close()
the client:
import socket
my_socket = socket.socket()
my_socket.connect(('the_real_ip', 9876))
the error I get is:
Traceback (most recent call last):
File "D:/Heights/Documents/Projects/Cyber/Password_Server.py", line 4, in <module>
server_socket.bind(('my_ip',9876))
File "D:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10049] The requested address is not valid in its context
You can use server_socket.bind(('0.0.0.0',9876)) and server_socket.bind(('',9876))
Check this link: https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1
To connect from outside your LAN, you have to connect to the router of that LAN using the 'outside IP' and make let the router formward the connection to the 'local IP'.
I have this code:
>>> import socket
>>> sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sck.connect(('loaclhost', 2525))
And I have this error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
sck.connect(('localhost', 2526))
socket.error: [Errno 10061] No connection could be made because
the target machine actively refused it
Please helpe me. Where can be error? How can I repair it?
try changing the connection to "localhost" instead of "loaclhost"
Your code and error don't match (spelling of localhost and port number), but the error message is due to no server listening on that port. Cut and paste the exact code and error message next time.