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
Related
my code is:
import socket
import os
s = socket.socket()
host = socket.gethostname()
port = 22
s.bind((host,port))
print("")
print("server currently running # ", host)
print("")
print("Waiting for any incoming connections...")
s.listen(1)
conn, addr = s.accept()
print("")
print(addr, "connected well")
while 1:
command = input("Enter command: ")
conn.send(command.encode())
conn.recv(1024)
print("command executed well")
The error given is:
Traceback (most recent call last):
File "/home/pi/Desktop/server.py", line 6 in <module>
s.bind((host,port))
PermissionError: [Errno 13] Permission denied
I am still quite new to using socket but I have made sure that my internet has no socket firewalls and the pc and raspberry pi should connect so could somebody please tell me what I'm doing wrong or point me in the right direction? Please and thank you.
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
import os
import socket
s =socket.socket()
port=8080
host=("(my ip)")
s.connect((host,port))
print("")
print("Connected to the server succefully")
#connection has been completed
#command receiving and execution
while 1:
command =s.recv(1024)
command = command.decode()
print("command recieved")
print("")
if command == "view_cwd":
files = os.getcwd()
files = str(files)
s.send("".encode())
s.send(command.encode())
print("Command has been executed successfully..")
else:
print("")
print("Command not recognised")
after running the code it gives me this error:
Traceback (most recent call last):
File "slave.py", line 7, in
s.connect((host,port))
ConnectionRefusedError: [Errno 111] Connection refused
You should make sure the destination port is open. For example by trying use:
telnet {host} {port}
You'll find that's no matter with Python or your script. It's just that the server is shutdown.
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
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.