Python socket not accepting port as variable from api - python

I am running a socket script to wait for a connection using a port number from an api response
SELENIUMPORT = cont["Ports"][0]["PublicPort"]
I am converting it to an int using a function then I'm passing it to socket but it errors with
client.connect(('192.168.33.10',SELENIUMPORT))
socket.error: [Errno 111] Connection refused

ECONNREFUSED (111)
Connection refused. This error can occur during an attempt to connect a TCP socket. It is reported when a reset or an unexpected SYNC message is received.
I pretty sure that you are going to have to put that in try loop and wait for the other end to be listening.
Edit:
From your comments you are going to have to print out the SELENIUMPORT at the same time that you have that port number from the program that is supposedly listening to check that they are identical each time.
Arguably, testing by adding let's say 10 to the port number should give you an identical error, ie nothing is listening on the other end.
Short of that, create a script that does listen on a given port and test against that i.e.
import os, socket, time
address = ('localhost',32840)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(1)
sc, client_address = s.accept()
while True:
print ("Connection made ", client_address)
n = 1
try:
data_recv = sc.recv(4096)
if len(data_recv) > 0:
print ("Received ", data_recv,n)
except:
print ("Disconnect")
break
Try running that and get your connection challenged code to connect to it or run something like:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = ('localhost',32840)
s.connect(address)
s.sendall('mystuff')

Related

Python socket throwing the following error ConnectionResetError: [Errno 54] Connection reset by peer

I have been trying to create a messaging service in python using sockets. I have written the code to host two connections and allow one to send messages to the other using username and host_addr.
But every time I try to connect the second client and send a message from the first getting the following error.
ConnectionResetError: [Errno 54] Connection reset by peer
Server.py
import socket
import _thread as thread
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 1237 # Port to listen on (non-privileged ports are > 1023)
user_mapping = {}
def on_new_client(conn, addr):
data = conn.recv(1024)
data = data.decode()
print(data)
print(user_mapping)
if data[:8] == "username":
user_mapping[data[9:]] = (addr, data[9:])
elif data[0] == "#":
for i in user_mapping.values():
if i[0] == addr:
from_user = i[1]
else:
str = "user not found"
conn.sendto(str.encode(), addr)
str = "%s:%s" % (from_user, data[data.find(":") + 1:])
conn.sendto(str.encode(), user_mapping[data[1:data.find(":")](0)])
else:
pass
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
while True:
conn, addr = s.accept()
thread.start_new_thread(on_new_client,(conn,addr))
s.close()
Client.py
import socket
HOST = "127.0.0.1" # The server's hostname or IP address
PORT = 1237 # The port used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
username = input("Enter user name")
str = "username: %s" % username
s.send(str.encode())
while True:
message = input(username+">")
s.send(message.encode())
data = s.recv(1024)
print(data)
Was hoping some would could answer why this is happening and guide me to any good links where there is Information on creating a messaging service in python.
Client is sending 2 messages and then receiving one.
But server just listen once and then send one or two packages.
Chronologically:
Client sends a package, and server reads it.
Then both client and server try to send a package. Both packages that won't meet a listening peer.
Then client try to receive a package, but server won't send (he already sent it before) or it may send but its too late because communication is already broken.
Concepts you may implement always:
If one talk, another one may listen.
If a package is mean to be sent, it shall be sent anyway. Dont let a 'if' statment that send package when at 'else' that does not (or viceversa).
==== EDIT ====
About solution:
You need to work with paralel loops.
Take a look at this code https://www.neuralnine.com/tcp-chat-in-python/
He uses two threads on client, one for keep listening for new server updates (messages for other people) and another one to wait input from user.

Python Socket Auto Reconnect

I'm a beginner in Python. So I wanted to make if a server shuts down, disconnects, the client just keeps connecting until the server is opened again. I get this error:
File "C:\Users\Laurynas\Desktop\project\client.py", line 24, in reconnect server1.connect((HOST, PORT)) OSError: [WinError 10056] A connect request was made on an already connected socket
Current client.py code:
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = socket.gethostbyname(socket.gethostname())
PORT = 8888
# Check at the first try
def connect():
try:
server.connect((HOST, PORT))
messages()
except ConnectionRefusedError:
print("reconnecting, please wait...")
time.sleep(0.1)
connect()
# Check at the second, third, etc.
def reconnect():
try:
server1.connect((HOST, PORT))
messages()
except ConnectionRefusedError:
print("reconnecting, please wait...")
time.sleep(0.1)
reconnect()
def messages():
while True:
try:
command = server.recv(1024).decode()
print(command)
except:
reconnect()
pass
connect()
With the exception of listening sockets that are used for many accepts, data sockets cannot be reconnected and reused. On the client side a new socket needs to be created for the new connection and on the server side a new accept needs to be made. The old sockets should also be closed to get them out of the kernel.
This poses a difficulty because a server won't automatically know which client is reconnecting and which higher level activity should be restarted. This has to be baked into the protocol you implement on top of the connection. In HTTP for instance, each GET/PUT/POST reidentifies itself so that the web server knows how to do that, perhaps using a cookie based session id.
Bottom line, you can't keep on calling server.connect to start it up again.

Send a string between python programs

I want to send some simple information, like an int or a string, between two Python programs. I thought of doing it by having the programs read and write from a single-line file. But that doesn't seem to work, since one file seems to block the file. Especially since I want to check for updates every 1/12 of a second or so.
If it did work, my idea use case would be for one program to send a message with
with open('input.py','w') as file:
file.write('hello')
and receive it with
with open('input.py','r') as file:
print(file.read())
I've been looking into how to do it with sockets, but every 'simple' tutorial seems to be targeted some much more complex use case. So how do I do what I need to do in a way that'll actually work?
The best route to go is to use the socket library. This creates a client-server connection, where you can send strings between programs from there.
server.py:
import socket
s = socket.socket()
print "Socket successfully created"
port = 12345 # Reserve a port on your computer...in our case it is 12345, but it can be anything
s.bind(('', port))
print "Socket binded to %s" %(port)
s.listen(5) # Put the socket into listening mode
print "Socket is listening"
while True:
c, addr = s.accept() # Establish connection with client
print 'Got connection from', addr
c.send('Thank you for connecting') # Send a message to the client
c.close()
client.py:
import socket
s = socket.socket()
port = 12345 # Define the port on which you want to connect
s.connect(('127.0.0.1', port)) # Connect to the server on local computer
print s.recv(1024) # Receive data from the server
s.close()
From the terminal/shell:
# start the server:
$ python server.py
Socket successfully created
Socket binded to 12345
Socket is listening
Got connection from ('127.0.0.1', 52617)
# start the client:
$ python client.py
Thank you for connecting
As you can see, the client was able to receive the string "Thank you for connecting" by the server, thanks to the send() and recv() methods from the socket library.

Python port scanner marks every port as closed when using for loop and range function

I am trying to make a simple Python port scanner script, connect to IP and port/s, if connection is successful, the port should be marked as open.
I have tried to omit the for loop and the range function, using the port number explicitly and it works, but for some reason it does not like the iteration thing, also tried to use connect_ex function and did not work.
server.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 1200))
s.listen(600)
print("Waiting for connections...")
while 1:
clientsocket, address = s.accept()
print(f"Connection from {address} has been established!")
clientsocket.send(bytes("Welcome to the server!","utf-8"))
portScanner.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for port in range(1000,1400):
try:
s.settimeout(3)
connection = s.connect(("127.0.0.1",port))
print(f"Port {port} is open!")
except:
print(f"Port {port} is closed.")
s.close()
I expect the output to be "Port 1200 is open!" among the lines of Port xxxx is closed., but the actual output is all the ports are marked as closed.
I believe the issue lies in the fact that you're trying to use the same socket for each connection. I'm able to reproduce your issue on a mac, though the issue might be platform dependent as the socket library calls the OS directly.
In your code the first exception thrown is [Errno 61] Connection refused and then everything after is [Errno 22] Invalid argument, so there is something in the state of the socket that is causing all other connection attempts to fail. Note that you're not calling s.close() in a finally block in the loop which you should be doing, but even doing that will give you a [Errno 9] Bad file descriptor error.
In any event, I was able to get this to work by creating a new socket for each connect attempt and properly closing it with a with block:
for port in range(1000, 1400):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(3)
connection = s.connect(("127.0.0.1", port))
print(f"Port {port} is open!")
except:
print(f"Port {port} is closed.")

Errno 111: Connection refused only in python IDLE

When I try to execute Python server socket program and client socket program (both running in same linux machine) in Linux terminal I don't get any error, server accepts connection and sends data to client successfully.
But when I execute the same programs in python IDLE I get "[Errno 111] Connection refused" error.
What is the difference in both execution?
I'm using serversock.bind(('',port#)) in server
and in client i'm using clientsock.connect(('localhost',port#))
Server.py
import socket
serversock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = ''
print host
port = 5800
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind((host,port))
serversock.listen(2)
try:
while True:
csock,addr = serversock.accept()
print 'Recieved address from %s '% str(addr)
csock.send('hello')
csock.close()
except Exception,e:
print e
client.py
import socket
try:
c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = 'localhost'
port = 5800
c.connect((host,port))
data = c.recv(1024)
print data
except Exception,e:
print e
finally:
c.close()
Server side you must use:
serversock.bind(('',port#)) # binds to port
serversock.listen(5) # actually listen to port and allow 5 incoming calls
conv = serversock.accept() # accept first incoming call
The connection will only be accepted after the listen call, before it, you have only bound a socket, but have not declared that you were ready to accept incoming calls, so they are refused.
With added code, another possible error cause is that you close connection (server side) immediately after sending data. That is bad: the close condition can destroy the socket before the data has actually been sent.
You should use a graceful shutdown:
server side:
csock.send('hello')
csock.shutdown(socket.SHUT_WR) # shutdown the socket
csock.read() # wait the close from peer
csock.close()
client side: you can leave the code as is in your use case you do not send anything to server, so when the client has finished reading it can safely close the socket
Anyway you should close serversock when everything is finished
try:
...
except ...:
...
finally:
serversock.close()

Categories