guys, I am looking for a way to connect my python socket programms securely.
I imagined something like the following:
from socket import *
server = socket(AF_INET, SOCK_STREAM, cert="cetificate.pem")
server.bind(("192.168.2.100",8080))
server.listen()
while True:
client, addr = socket.accept()
client.sendall(b"Hello, World!")
I have found an alternative solution on github, but it is very complex and i dont understand it.
Please help me guys
I have found a solution:
import socket
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('/path/to/certchain.pem', '/path/to/private.key')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind(('127.0.0.1', 8443))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
conn, addr = ssock.accept()
Related
I am trying to build a client and server script to interact with each other using sockets, but I am confused on where to find the IP that I should put in place of 'localhost' in this example. Someone please help direct me on where to find the thing to put in place of it.
Server side:
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(5) # become a server socket, maximum 5 connections
while True:
connection, address = serversocket.accept()
Client Side:
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
So far, I have been working with Sockets. One socket for sending and receiving. I've been told that that is stupid, however I was not able to find the correct way to use socketpairs on client and server from ground up. I will give my current way, and hope someone could tell me how to do socketpairs with this. Using TCP by the way.
#client
import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "127.0.1.1"
port = 22222
my_socket.connect((ip, port))
#server
import socket
import multiprocessing
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip_address = socket.gethostbyname(hostname)
port = 22222
s.bind((ip_address, port))
s.listen(20)
connections=[]
connection_threads=[]
while True:
clientsocket, address = s.accept()
#does this create a new socket for every incoming connection by the way?
new_client = client.Client(address, clientsocket)
connections.append(new_client)
new_process = multiprocessing.Process(target=client_butler.connect_client, args=(new_client,))
new_process.start()
connection_threads.append(new_process)
My end goal would be that the server has one socketpair for every client. I know that this is probably not good implementation as it is completely ignoring errorhandling and so on, but I will look at that in the future as all I am trying to do is me self teaching this. So the question is, how would I transform this into working with socketpairs?
Problems I have:
How do I connect the socketpair as in my client via my_socket.connect?
If I just connect both sockets individually, how do I 'reassemble' the pair in the server
Am I even doing this correctly, or is this the completely wrong approach?
Also: I checked this out, but even if I could copy the code and it would work I wouldn't know why it would work, which isn't what I want, I don't want this to work I'd also like to learn from this.
Thanks!
Referenced from Computer Networking: A Top-Down Approach, 7th Edition:
client.py:
from socket import *
serverName = "127.0.0.1"
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
sentence = input('Input lowercase sentence: ')
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print('From Server: ', modifiedSentence.decode())
clientSocket.close()
server.py
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())
connectionSocket.close()
I am attempting to connect a simple server and client from two computers on the same network. Both the client and server cannot 'find' each other, as they do not move past .connect() and .accept() respectively. What am I doing wrong?
(Windows 10)
Server:
import socket
HOST = socket.gethostname() #Returns: "WASS104983"
#I have also tried socket.gethostbyname(socket.gethostname)), returning: "25.38.252.147"
PORT = 50007
sock = socket.socket()
sock.bind((HOST, PORT))
sock.listen(5)
print("Awaiting connection... ")
(clnt, addr) = sock.accept()
print("Client connected")
…
and Client:
import socket
HOST = "WASS104983" #Or "25.38.252.147", depending on the servers setup
PORT = 50007
sock = socket.socket()
print("Attempting connection... ")
sock.connect((HOST, PORT))
print("Connected")
…
I have gotten this to work before so I am not sure why it's not now.
I know there are a few questions of this calibre, but none seem to cover my problem.
Also, a wifi extender should not interfere with local transmissions should it?
I have always seen servers setup as such:
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print("[*] Listening on {}:{}".format(bind_ip, bind_port))
def handle_client(client_socket):
request = client_socket.recv(1024)
print('received: {}'.format(request))
client_socket.send(b'ACK!')
client_socket.close()
while True:
client, addr = server.accept()
print("[*] Accepted connection from: {}:{}".format(addr[0], addr[1]))
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()*
Where I think an important distinction from your post may be that the server accepting connections is within an infinite loop. Have you tried this?
So i would like to create a small online game. (I am a complete noob with networking)
I got a server-client connection going if they are both on the same network but would need to do it over the internet. How do i do this?
server.py
import socket
s = socket.socket()
host = "0.0.0.0"
port = 5000
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print("Connection from", addr)
c.close()
client.py
import socket
s = socket.socket()
host = "192.168.1.66"
port = 5000
s.connect((host, port))
s.send(bytearray("Message", "ascii"))
I have tried using my public IP but i cannot connect.
I'm trying the following client and server chat program. Although I get an error whenever I try to run the server program, when the client program runs it stays on a blank screen not allowing me to type anything. I've tried running server first and running client first and I get the same results. I can't read the error from the server program because it flashes the error and closes the window. Here is my code:
server:
#server
import socket
import time
HOST = ''
PORT = 8065
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
client:
#client
import socket
import time
HOST = "localhost"
PORT = 8065
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOST,PORT))
s.sendall('Helloworld')
data = s.recv(1024)
s.close()
print 'Recieved', repr(data)
Im not an expert but I was able to make your examples work by changing the socket from datagram to stream connection, and then encoding message being sent because strings aren't supported (although this might not effect you since I think that change was made in Python 3...I'm not 100% sure).
I believe the main issue is that you're trying to listen() but SOCK_DGRAM (UDP) doesn't support listen(), you just bind and go from there, whereas SOCK_STREAM (TCP) uses connections.
If you're just trying to get the program going, use the below code, unless there is a specific reason you'd like to use SOCK_DGRAM.
The code is below:
client
#client
import socket
import time
HOST = "localhost"
PORT = 8065
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST,PORT))
test = 'Helloworld'
s.sendall(test.encode())
data = s.recv(1024)
s.close()
print 'Recieved', repr(data)
server
#server
import socket
import time
HOST = ''
PORT = 8065
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by', addr)
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()