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
Related
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
I am trying to send packet data between two sockets using socket programming in python, basically the instructions Ive been given is to make 6 routers (im just doing two right now) and have them send packets to eachother and such, the instructions specify that I have to run the second router first then run the first (the first is the one that is reading the packets.csv file) But whenever I run the second router and then the first I get this error form the first
Traceback (most recent call last):
File "router1_skeleton.py", line 185, in <module>
socket = create_socket(host,port)
File "router1_skeleton.py", line 15, in create_socket
soc.bind((host,port))
OSError: [Errno 98] Address already in use
If I understand correctly the way to make this work is to set up the socket in router 2 which I have here
# 1. Create a socket.
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. Try connecting the socket to the host and port.
soc.bind((host,port))
soc.listen(5)
try:
conn,addr = soc.accept()
except:
print("Connection Error to", port)
sys.exit()
# 3. Return the connected socket.
return soc
And then for my first router I write
def create_socket(host, port):
# 1. Create a socket.
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. Try connecting the socket to the host and port.
soc.bind((host,port))
soc.listen(5)
try:
#print("hello")
conn,addr = soc.accept()
print("Received connection fron:", addr)
conn.send('Thank you for connecting'.encode())
except:
print("Connection Error to", port)
sys.exit()
# 3. Return the connected socket.
#print(soc)
return soc
Then I send data using soc.send(new_packet.encode())
and router 2 should be getting the data with this line and it should be working but it breaks along here
while True:
# 8. Accept the connection.
connection, address = soc.accept()
print("Accepted a connection from: ", address)
data = soc.rev(1024)
print(data.decode())
##ip, port =
print("Connected with " + ip + ":" + port)
Any suggestions are very welcome!
EDIT: Broken pipe / Transport endpoint error
if newport == 2:
print("sending packet", new_packet, "to Router 2")
socket.send(new_packet.encode())
write_to_file("output/sent_by_router_1.txt", new_packet, 2)
I use this to send the data to router 2 and this should receive it
while True:
# 8. Accept the connection.
connection, address = soc.accept()
print("Accepted a connection from: ", address)
data = soc.recv(1024)
print(data.decode())
This is the error I get
File "router1_skeleton.py", line 237, in <module>
socket.send(new_packet.encode())
BrokenPipeError: [Errno 32] Broken pipe
im making a Reverse Shell. and in the server.py file i got this error.
i has trying in de socket_bind() s.bind((host, port))
My code:
def socket_bind():
try:
global host
global port
global s
print("Binding socket to port: " + str(port))
s.bind((host, port))
s.listen(5)
except socket.error as msg:
print("Socket binding error: " + str(msg) + "\n" + "retrying...")
socket_bind()
my error:
Binding socket to port: 90
Traceback (most recent call last):
File "c:/ReverseShell/server.py", line 50, in <module>
main()
File "c:/ReverseShell/server.py", line 47, in main
socket_bind()
File "c:/ReverseShell/server.py", line 21, in socket_bind
s.bind((host, port))
TypeError: an integer is required (got type str)
how can i fix this error?
I know where you get this code. I don't know how to fix it, but you could use the code below for server.
# first import all require module
import socket # For building tcp connection
import os # Using this module for basic operation
os.system ("clear || cls") # it clear the terminal screen
def connect ():
s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # START a socket subject s
s.bind (("192.168.0.200", 9999)) # Define IP and Port
s.listen (1) # listen METHOD for pass the parameter as 1
print ('[+] Listening for incoming TCP connection on port 8080')
conn, addr = s.accept () #I want my server to accept CLIENT AND IP ADRESS BY ACCEPT METHOD
print ('[+] We got a connection from: ', addr)# After accpeted, print out the result
ter = 'terminate'
while True: #while connection is true
command = input ("\nShell>") # Get user input and store it in command variable
if ter in command: # If we type terminate command, so close te connection and break the loop
conn.send (ter.encode('utf-8')) #to send data to client with conn.send()
conn.close ()
break
else:
conn.send(str.encode (command)) #here we will send the command to the target send commands from server to client using python socket
client = str(conn.recv(1024).decode("utf-8"))
print (client) # print the result that we got back
def main():
connect()
main ()
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
I am making a socket connection and when I try and connect to a different computer it returns the error:
Traceback (most recent call last):<br>
File "C:\Python34\Scripts\stuff\server.py", line 9, in <"module"><br> ##without the quotations the word would not appear because of the "<>" in html code.<br>
s.bind((HOST,PORT))<br>
OSError: [WinError 10049] The requested address is not valid in its context
Here is the code for the server/receiver:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
HOST = "192.168.1.157"
PORT = 5454
s.bind((HOST,PORT))
s.listen(1)
while True:
connection,client = s.accept()
try:
while True:
print(bytes.decode(connection.recv(999)))
except:
connection.close()
And here is the code for the client/sender:
import socket
HOST = input("Connect to: ")
PORT = 5454
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST,PORT))
while True:
text = str.encode(input("Send: "))
s.sendall(text)
It all works if I use 'localhost' for HOST in both server and client but the idea is that the receiver on someone's computer will connect to my PC and I have to input the IP of their PC for the sender.
Is it something I am doing wrong or something I missed? I am sorta new to this but I have looked at lots of tutorials and to me, this should work.
Any help is greatly appreciated.