How to open an openssl s_client socket in Python - sockets

i am new to Python, i need to open a socket using Openssl in Python for the below command, which is working directly.
openssl s_client -CAfile trust.cer -cert server.cer -key server_key.pem -port 31114 -host 10.238.110.110 -tls1 -quiet –crlf
this is required for firing some MML Commands using the CLI on the server 10.238.110.110 in our private network
import socket
import ssl
import sys
import os
class Client:
def __init__(self,host,port):
self.host = host
self.port = port
self.sock = None
self.connected = False
def connect(self):
data = []
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(0)
self.sock.settimeout(15)
try:
self.sock.connect((self.host,self.port))
#self.connected = True
data = self.sock.recv(1024)
if ( not data):
self.connected = False
print("not connected")
else:
self.connected = True
print("connected")
except IOError as e:
self.connected = False
print("error")
Its always printing not connected. on Wireshark also it shows the server is finishing the connection.
Please help me with the code

def connect(self):
data = []
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(0)
self.sock.settimeout(15)
# WRAP SOCKET
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")
try:
wrappedSocket.connect((self.host,self.port))
#self.connected = True
data = self.sock.recv(1024)
if ( not data):
self.connected = False
print("not connected")
else:
self.connected = True
print("connected")
except IOError as e:
self.connected = False
print("error")
try this

I was able to connect to socket using the below code. Thanks for the support everyone.
import socket
import ssl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr=("10.200.200.101",31114)
wrapper = ssl.wrap_socket(s)
wrapper.connect(addr)

Related

OSError: [WinError 10038] An operation was attempted on something that is not a socket what is the problem?

im running a chat app on my computer and i tried to send a message and this ERROR accured. it suppose to send a " will joind the chat" and another message " connected to server" after that i should send my message to the group and it didnt worked.
any ideas?
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import socket
import threading
kivy.require("1.9.0")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class MyRoot(BoxLayout):
def __init__(self):
super(MyRoot, self).__init__()
def send_message(self):
client.send(f"{self.nickname_text.text}: {self.message_text.text}".encode('utf-8'))
def connect_to_server(self):
if self.nickname_text != "":
client.connect((self.ip_text.text, 9999))
message = client.recv(1024).decode('utf-8')
if message == "NICK":
client.send(self.nickname_text.text.encode('utf-8'))
self.send_btn.disabled = False
self.message_text.disabled = False
self.connect_btn.disabled = True
self.ip_text.disabled = True
self.make_invisible(self.connection_grid)
self.make_invisible(self.connect_btn)
thread = threading.Thread(target=self.receive)
thread.start()
def make_invisible(self, widget):
widget.visible = False
widget.size_hint_x = None
widget.size_hint_y = None
widget.height = 0
widget.width = 0
widget.text = ""
widget.opacity = 0
def receive(self):
stop = False
while not stop:
try:
message = client.recv(1024).decode('utf-8')
self.chat_text.text += message + "\n"
except:
print("ERROR")
client.close()
stop = True
class TraboChat(App):
def build(self):
return MyRoot()
trabo_chat = TraboChat()
trabo_chat.run()
serve.py:
import socket
import threading
HOST = socket.gethostbyname(socket.gethostname())
PORT = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
nicknames = []
def broadcast(message):
for client in clients:
client.send(message)
def handle_connection(client):
stop = False
while not stop:
try:
message = client.recv(1024)
broadcast(message)
except:
index = clients.index(client)
clients.remove(client)
nickname = nicknames[index]
nicknames.remove(nickname)
broadcast(f"{nickname} left the chat!".encode('utf-8'))
stop = True
def main():
print("server is running..")
while True:
client, addr = server.accept()
print(f"connected to {addr}")
client.send("NICK".encode('utf-8'))
nickname = client.recv(1024).decode('utf-8')
nicknames.append(nickname)
clients.append(client)
print(f"Nickname is {nickname}")
broadcast(f"{nickname} join the chat!")
client.send("You are now connected".encode('utf-8'))
thread = threading.Thread(target=handle_connection, args=(client,))
if name == 'main':
main()

(Python)What do I do to stop receiving this error? [WinError 10013]An attempt was made to access a socket in a way forbidden by its access permissions

I'm trying to make a python socket client/server chat app, but can't seem to work around this error. I've used netstat -a|findstr 7000 to make sure the port was available, and it was. I don't have any antivirus running besides Windows Security. I also tried it after turning off all firewalls on my computer--have not messed with firewall settings in my router. I'm running both files in Anaconda Powershell Prompt as admin.
Any help is appreciated.
Code for server.py :
from socket import AF_INET, socket, SOCK_STREAM
import threading
BUFSIZ = 1024
FORMAT = 'utf-8'
SERVER = '192.168.1.10'
ADDR = (SERVER, 7000)
server = socket(AF_INET, SOCK_STREAM)
server.bind(ADDR)
sockets = {server}
clients = {}
def accept_incoming_connections():
while True:
client, client_addr = server.accept()
print ("%s:%s has connected." % client_addr)
client.send(bytes("Enter your name and press enter."), FORMAT)
sockets[client] = client_addr
# client_thread = threading.Thread(target=handle_client, args=(client,)).start()
def handle_client(client):
client_name = client.recv(BUFSIZ).decode(FORMAT)
client.send(bytes("Type .exit when you're ready to quit.", FORMAT))
msg = "%s has entered the chat." % client_name
broadcast(bytes(msg, FORMAT))
clients[client] = client_name
while True:
msg = client.recv(BUFSIZ)
if msg == bytes(".exit", FORMAT):
client.send(bytes(".exit", FORMAT))
client.close()
del clients[client]
broadcast(bytes("%s has left the chat." % client_name, FORMAT))
break
else:
broadcast(msg, client_name)
def broadcast(msg, name=""):
for client in clients:
client.send(bytes(name, FORMAT) + msg)
if __name__ == "__main__":
server.listen()
print("Awaiting connection.")
accept_incoming_connections()
Code for client.py :
import socket
import threading
BUFSIZ = 1024
FORMAT = 'utf-8'
SERVER = '192.168.1.10'
ADDR = (SERVER, 7000)
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.connect(ADDR)
def receive():
msg = client_sock.recv(BUFSIZ).decode(FORMAT)
print(msg)
def send():
msg = input('Enter msg: ')
msg.encode(FORMAT)
client_sock.send(msg)
if msg == ".exit":
client_sock.close()
receive_thread = Thread(target=receive)
receive_thread.start()
EDIT: I fixed it. I set SERVER to 'localhost' and PORT to 9999 for both server and client.

TCP Socket Programming in Python3: Recieving alternate message at Server Side

I coded server side and client side and I am using TCP connection.
Server Side:
import socket
def Main():
host = '192.168.1.3'
port = 5014
s = socket.socket()
s.bind((host,port))
s.listen(1)
c,addr = s.accept()
while True:
if c.recv(1024).decode('UTF_8') == 'Q':
print('Terminating Connection...')
c.close()
s.close()
break;
else:
print(c.recv(1024).decode('UTF_8'))
if __name__ == '__main__':
Main()
Client Side:
import socket
def Main():
host = '192.168.1.3'
port = 5014
c = socket.socket()
c.connect((host,port))
data = ''
while True:
if data == 'Q':
break;
else:
data = input('->')
c.send(data.encode('UTF_8'))
print('Terminating Connection...')
c.close()
if __name__ == '__main__':
Main()
It seems everything is correct, but while running , when I am giving input at client side, message is printing up only alternatively.
Example:
Client Side Input:
->hello1
->hello2
->hello3
->hello4
->Q
Terminating Connection...
Server Side Output:
hello2
hello4
Terminating Connection...
I am not getting why hello1 and hello3 are not getting printed.
Please help. Thanks in Advance

Establish a connection through socks with socket library

I have this script that simply uses a list of socks and tries to connect to a host. But it always shows this error:
File "*.py", line 38, in requestsocks
s.connect((host, port))
File "/home/*/.local/lib/python3.5/site-packages/socks.py", line 729, in connect
_BaseSocket.connect(self, proxy_addr)
TypeError: an integer is required (got type str)
Here is the code:
import socket,socks,random
def checkurl():
global url
global url2
global urlport
url = input("Insert URL: ")
if url[0]+url[1]+url[2]+url[3] == "www.":
url = "http://" + url
elif url[0]+url[1]+url[2]+url[3] == "http":
pass
else:
url = "http://" + url
try:
url2 = url.replace("http://", "").replace("https://", "").split('/')[0].split(":")[0]
except:
url2 = url.replace("http://", "").replace("https://", "").split('/')[0]
try:
urlport = url.replace("http://", "").replace("https://", "").split('/')[0].split(':')[1]
except:
urlport = "80"
def proxylist():
global entries
out_file = str(input("Enter the proxy list: "))
entries = open(out_file).readlines()
requestsocks()
def requestsocks():
while True:
proxy = random.choice(entries).strip().split(':')
host = str(url2)
port = int(urlport)
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy[0], proxy[1], True)
s = socks.socksocket()
s.connect((host, port))
s.sendall('Hello world')
checkurl()
proxylist()
I don't know how can I solve this. Ideas?
P.S. I've taken the socks list from here: https://www.socks-proxy.net/
I haven't done much with the sockets library, but it's asking for an integer for both host and port. Try getting the IP like this.
Add this ------> host = socket.gethostbyname(host)
def requestsocks():
while True:
proxy = random.choice(entries).strip().split(':')
host = str(url2)
# convert url to IP address
host = socket.gethostbyname(host)
port = int(urlport)
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy[0], proxy[1], True)
s = socks.socksocket()
s.connect((host, port))
s.sendall('Hello world')

Python code after sockets connection executed only once

What are the intentions of this program:
I want to send some commands from a client to a server using sockets, the server then send these command to an Arduino using serial. And another thing that I want the server to do in the future is that periodically sends other commands to the Arduino without getting any input from the client, so the sockets needs to be non-blocking or there needs to be another way to run the code separately from the sockets code.
The problem is that the part that is supposed to send the command to the Arduino only runs once.
What I have come up with after playing with the debugger in Pycharm, is that the problem is that the following line blocks after a connection has been established, and thus not allowing the rest of the code to be run.
conn, addr = s.accept()
Is this correct, or is there something else wrong?
I have tried to set the socket to non-blocking but when I do this I get an error.
"BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately"
I have some basic knowledge of C/C++ and C# and am new to Python.
server.py
import socket
import serial
import sys
from _thread import *
import threading
import queue
# command that the client sends are "ON" and "OFF"
class serialConnect:
comPort =' '
baudrate = 115200
myserial = serial.Serial('COM5', baudrate)
def serialstart(self):
# self.comPort = input('Comport: ')
try:
self.myserial.open()
except IOError:
print('Port is already open!')
def serialRead(self):
data = self.myserial.read(16)
data.decode('UTF-8')
return data
def serialWrite(self, data):
data += '\n' #the arduino needs a \n after each command.
databytes = data.encode('UTF-8')
self.myserial.write(databytes)
print('send data: ', databytes)
def threaded_client(conn, dataqueue):
data = {bytes}
conn.send(str.encode('welcome, type your info \n'))
while True:
data = conn.recv(2048)
if not data:
break
reply = 'server output: ' + data.decode('UTF-8')
dataqueue.put(data.decode('UTF-8'))
print("Items in queue: ",dataqueue.qsize())
#conn.sendall(str.encode(reply))
print("Recieved data in threaded_client: ", data.decode('UTF-8') + '\n')
conn.close()
def Main():
ser = serialConnect()
host = ''
port = 5555
dataRecieved = 'hello'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.setblocking(1) #when set to non-blocking error occurs : "BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately"
workQueue = queue.Queue(10)
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5)
print('waiting for a connection')
while True:
try:
conn, addr = s.accept() #once connection is established it blocks?
print('connected to: ' + addr[0] + ':' + str())
t = threading.Thread(target=threaded_client, args=(conn, workQueue))
t.daemon = True
t.start()
except:
e = sys.exc_info()
print('Error:', e)
# This section of code is only run once, doesn't matter if put inside try block or not. :(
dataRecieved = workQueue.get()
print('The recieved data: ', dataRecieved)
ser.serialstart()
ser.serialWrite(dataRecieved)
if __name__ == '__main__':
Main()
client.py
import socket
def Main():
host = '127.0.0.1'
port = 5555
message = "<,R,G,B,>"
mySocket = socket.socket()
mySocket.connect((host, port))
while message != 'q':
message = input(" -> ")
mySocket.send(message.encode())
mySocket.close()
if __name__ == '__main__':
Main()
Arduino Code
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int LEDpin = 10;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(10, OUTPUT);
Serial.begin(19200);
}
// the loop function runs over and over again forever
void loop() {
serialEvent();
if(stringComplete){
Serial.println(inputString);
if(inputString == "ON\n"){
digitalWrite(LEDpin, HIGH); // turn the LED on (HIGH is the voltage level)
}
if(inputString == "OFF\n"){
digitalWrite(LEDpin, LOW); // turn the LED off by making the voltage LOW
}
inputString = "";
stringComplete = false;
}
}
void serialEvent()
{
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Refactored server code for anyone that is interested in it.
I am not sure if this is up to standard, but it is working.
import serial
import socket
import queue
import sys
import threading
class serialConnect:
comPort = 'COM5'
baudrate = 115200
myserial = serial.Serial(comPort, baudrate)
def serial_run(self):
# self.comPort = input('Comport: ')
try:
if not self.myserial.isOpen():
self.myserial.open()
else:
print('Port is already open!')
except IOError as e:
print('Error: ', e)
def serial_read(self):
data = self.myserial.read(16)
data.decode('UTF-8')
return data
def serial_write(self, data):
data += '\n' #the arduino needs a \n after each command.
databytes = data.encode('UTF-8')
self.myserial.write(databytes)
print('send data: ', databytes)
class socketServer:
host = ''
port = 5555
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.setblocking(1)
data_queue = queue.Queue(1)
def __init__(self):
try:
self.soc.bind((self.host, self.port))
except:
print('Bind error: ', sys.exc_info())
self.soc.listen(5)
def socket_accept_thread(self):
while True:
try:
print('Waiting for a new connection')
conn, addr = self.soc.accept()
client_thread = threading.Thread(target=self.threaded_client, args=(conn, self.data_queue))
client_thread.daemon = True
client_thread.start()
except:
print('Accept thread Error: ', sys.exc_info())
def threaded_client(self, conn, data_queue):
# conn.send(str.encode('welcome, type your info \n'))
try:
while True:
data = conn.recv(2048)
if not data:
break
# reply = 'server output: ' + data.decode('UTF-8')
data_queue.put(data.decode('UTF-8'))
print("Items in queue: ", data_queue.qsize())
# conn.sendall(str.encode(reply))
print("Received data in threaded_client: ", data.decode('UTF-8'))
except:
print("Error: ", sys.exc_info())
conn.close()
def get_data(self):
data = self.data_queue.get()
return data
def Main():
server = socketServer()
arduino_conn = serialConnect()
accept_thread = threading.Thread(target=server.socket_accept_thread)
data_received = 'Nothing received'
while True:
if not accept_thread.is_alive():
accept_thread.daemon = True
accept_thread.start()
arduino_conn.serial_run()
data_received = server.get_data()
arduino_conn.serial_write(data_received)
if __name__ == '__main__':
Main()