python socket package [WinError 10022] , invalid argument was supplied - python

import socket
from _thread import * #setting up a connection or using a port on
import sys # our network to look for certain connections
#34:06
server = "redacted IP"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # "AF_INET" is the type of socket and "SOCK STREAM" is how the data comes in
try:
s.bind((str ("redacted IP"), 5555))
except socket.error as e:
str(e) #check for errors
s.listen(2) # only want two port connections "player 1" and "player 2" therefore only 2 client on the socket
i've tried running this part of a server python file to create an online connection however i keep getting this error.
Traceback (most recent call last):
File "C:\Users\Phoen\PycharmProjects\chessProject1\server.py", line 16, in <module>
s.listen(2) # only want two port connections "player 1" and "player 2" therefore only 2 client on the socket
OSError: [WinError 10022] An invalid argument was supplied
i can't find an invalid argument in my code

Related

i got error 10061 in python

im trying to send a message to myself in python but the client code gives me error 10061 the server works properly it worked just fine but than it suddenly started giving me the error. i tried changing the port but it still gives me the same error
the server
from socket import *
from datetime import *
def main():
s = socket()
client_socket = None
s.bind(("127.0.0.1",8200))
print "1:time"
print "2:get list of files"
print "3:download file"
print "4:quit"
input()
s.listen(1)
client_socket, client_address = s.accept()
strn=client_socket.recv(4096)
if int(strn[0])>4 or int(strn[0])<1:
print "please send a num between 1-4"
elif int(strn[0])==1:
print datetime.time(datetime.now())
elif int(strn[0])==2:
folder=raw_input("enter the name of the folder")
dir(folder)
client_socket.close()
s.close()
input()
if name == '__main__':
main()
the client
from socket import *
def main():
s = socket()
s.connect(("127.0.0.1",8200))
buf = raw_input()
s.send(buf)
s.close()
input()
if name == '__main__':
main()
the error
Traceback (most recent call last):
File "D:\client.py", line 10, in <module>
main()
File "D:\client.py", line 4, in main
s.connect(("127.0.0.1",8200))
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10061] No connection could be made because the target machine actively refused it
10061 error occurs when the target machine refuses a connection.
In your case the most likely reason is the "IP" in s.bind and s.connect try putting an actual IP or 127.0.0.1. It should work
never mind i fixed it the problem was that the input was before the listen and it stalled the program thanks everyone

Python VNC ip scanner error

Hello i'm so confused on why i am getting this error
Code:
#!/usr/bin/python -w
import random
import socket
from random import randint
username = 'admin'
password = 'admin'
print 'Format:'
print '101.109'
range = raw_input("Range: ")
def main():
return '%s.%i.%i' % (range, rand(), rand())
def rand():
return randint (1,254)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
print 'Scanning %s:%s - %s' % (username, password, main())
port = (5900)
s.connect((main(), port))
Error code:
Format:
101.109
Range: 101.109
Scanning admin:admin - 101.109.154.9
Traceback (most recent call last):
File "C:\Users\Aries\Desktop\crap\Reflect.py", line 24, in <module>
s.connect((main(), port))
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
So my friend wanted me to make a VNC IP scanner so thats what im doing and im making it try to connect to it finds actual VNC ips but when its doing that i get an error as you see at the top
EDIT: MORE INFO
I need to know how i can make it not give me an error if the connection is not up
Wrap your connect in a try/except block:
Host = main()
try:
s.connect((Host, port))
print "Port {} is open on host {}".format(port, Host)
except:
print "Connection failed" # or just pass

Getting error while binding a socket

I am scripting a server to use at many things, gaming, data-transfer, chatting etc.
My problem is i am getting this error:
Traceback (most recent call last):
File "server.py", line 11, in <module>
s.bind((host, port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: an integer is required
I am at the beginning of my server script so far and i scripted many networking scripts before. There shouldnt be any problem. I tried this script both on my local and on my servers and still same resuly and the exact same error. I will really appreciate any kind of help.
Here is my code:
#!/usr/bin/python
# This is server file
import socket
# server & connection settings
host = '127.0.0.1'
port = '5002'
s = socket.socket() # Creating socket object
s.bind((host, port))
s.listen(10)
# server & connection settings
while True:
c,addr = s.accept() # Establish connection with clients.
print 'Got connection from ', addr # Print ip adress of the recently connected client.
c.send('You succesfully established connection with our servers.') # Send socket to the client.
print 'Socket had been sent to the client: ', addr # Print to the server console that we succesfully established connection with the client
c.close() # Close the client connection. Bye, bye! /// Will delete this part when the time come

socket.error: [Errno 111] when trying to connect to a socket

I was trying to write a code where a client connects to server on a default port number, the server then sends another port number to the client. The client now connects to the new port number.
Client:
import socket
import sys
import os
import signal
import time
s = socket.socket()
s.connect(("127.0.0.1", 6667))
line = s.recv(1024)
if line.strip():
port = int(line)
s.close()
soc = socket.socket()
soc.connect(("127.0.0.1", port))
print soc.recv(1024)
soc.close()
else:
s.close()
Server:
import socket
import sys
import os
import signal
import time
port = 7777
s = socket.socket()
s.bind(("127.0.0.1", 6667))
s.listen(0)
sc, address = s.accept()
print address
sc.send(str(port))
sc.close()
s.close()
sock = socket.socket()
sock.bind(("127.0.0.1", port))
soc, addr = sock.accept()
print addr
soc.send("Success")
soc.close()
sock.close()
When I execute this code, I am getting following errors on client and server sides.
Server:
('127.0.0.1', 36282)
Traceback (most recent call last):
File "server.py", line 17, in <module>
soc, addr = sock.accept()
File "/usr/lib/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 22] Invalid argument
Client:
Traceback (most recent call last):
File "client.py", line 13, in <module>
soc.connect(("127.0.0.1", port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
Can someone explain me the reason for these errors and provide a solution for these errors.
Before you can listen to a TCP/IP socket (a connection based streaming socket) you need to use bind to assign a socket (created with socket.socket()) . Then you need to do listen to prepare it for incoming connections and then finally you do accept on the prepared socket.
You appear to be missing sock.listen(0) after your call to sock.bind(("127.0.0.1", port)). The Python documentation is short on details but it does say this about TCP/IP:
Note that a server must perform the sequence socket(), bind(), listen(), accept() (possibly repeating the accept() to service more than one client), while a client only needs the sequence socket(), connect(). Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().
Python bases its socket module on a Berkeley Socket model. You can find some more detailed information on Berkeley Sockets at this link . In particular it says this about bind:
bind() assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connections to other hosts.
Also consider what would happen if your client gets sent a port number (and tries to connect) before the server starts listening for connections (on port 7777 in this case). Although not the cause of your problems, I wanted to point out the scenario for completeness. Something you may consider is not closing the port 6667 socket until after you have called listen on the port 7777 socket. After calling listen you can then close down the first socket. On the client after reading the port you can wait until the first connection (port 6667) is closed down by the server and then connect to port 7777.

Python 2.7 / Linux: socket library binding type error

I am attempting to write a very simple server in python.
import socket
import sys
# Create a TCP/IP socket to listen on
server = socket.socket(socket.SOL_SOCKET, socket.SOCK_STREAM)
# Prevent from 'address already in use' upon server restart
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind the socket to port 8081 on all interfaces
server_address = ('localhost', 8081)
print 'starting up on %s port %s' % server_address
server.bind(server_address)
I have read what I think to be correct documentation for the socket library, and it suggests that the server.bind() takes an argument of a tuple. However, I get this error:
starting up on localhost port 8081
Traceback (most recent call last):
File "pyserver.py", line 14, in <module>
server.bind(server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: argument must be string or read-only character buffer, not tuple
I have changed the argument to only a string, as the error warning suggests, and I get a
[Errno 98] Address already in use
error. I thought that the 8th line was in place to prevent that. What is going on?
The first argument to the socket.socket should be address family:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
^^^^^^^^^^^^^^
Except that, your code should work.
Reason of the error message: argument must be string ...
In Linux, the value of the socket.SOL_SOCKET is 1 which is equal to the value of socket.AF_UNIX. Unix domain socket (AF_UNIX) use path (string) as a address
>>> import socket
>>> socket.AF_UNIX
1
>>> socket.SOL_SOCKET
1
UPDATE
Regarding Already already in use error, see SO_REUSEADDR and AF_UNIX.

Categories