It seems like socket.bind isn't in the socket module - python

Here is the error that it is showing
I use windows 7 running python 3.8.6. But it is showing that socket module does not have socket.bind.

you must create socket object first. then you can call socket.bind which state the 'socket' or 's' here as an object, as this example
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)

Try socket.socket.bind instead.
Or change import to from socket import socket

Related

Print is not showing result when executing this simple socket connection code in python. Why is the print not showing result?

I have the following client/server python code. The print line from the server where i used f-string interpolation does not show anything when the code is executed and I would like to know why?
I followed this tutorial and on their end the print line shows result.
tutorial: https://www.tutorialspoint.com/python/python_networking.htm
line: Got connection from ('127.0.0.1', 48437)
my server code:
#!/usr/bin/python
# Server.py
# Import socket module
import socket
# Create a socket object
s = socket.socket()
# Get local machine name
host = socket.gethostname()
# Reserve a port number for the socket service
port = 22226
# Bind the host address and the port number
s.bind((host, port))
# Listens for the client connections made for the socket.
# The argument shows maximum number of queued connections and it is 1 at minimum
s.listen(5)
while True:
# Establish connection with client.
c, addr = s.accept()
print(f"Got connection from {addr}")
c.send(b'Thank you for connecting')
c.close() # Close the connection
my client code:
#!/usr/bin/python
# client.py
# Import socket module
import socket
# Create a socket object
s = socket.socket()
# Get local machine name
host = socket.gethostname()
# Reserve a port number for the socket service
port = 22226
# Connect the host address and the port number
s.connect((host, port))
# Read at most 1024 bytes
print(s.recv(1024))
s.close() # Close the socket when done

Undefined name error in python socket module

I am trying to create a simple web server in python. I've imported the two modules I needed but I keep getting 'undefined name' error for AF_INET and SOCK_STREAM. I am fairy new to this so any help would be appreciated.
from socket import socket
import sys
serverPort = 6789
serverSocket = socket(AF_INET, SOCK_STREAM)
Having Context managers while dealing with Python sockets is safer and better approach, you can do it like that:
import socket
import sys
serverHost = '127.0.0.1'
serverPort = 6789
## Context manager to allow you to allocate and release resources
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
## Bind it to the specified IP and PORT
s.bind((serverHost, serverPort))
## Listen on the server port to enable accepting connections
s.listen()
## Then accept connections to the server
conn, add = s.accept()

Connection using "socket" library

I am working on a python program which will allow me to send and receive data between PCs. I have managed to get it to work in my local network and wanted to set it up to be able to connect from the internet. I opened the port 1234 in my router and used the local IP on the server.py and puclic IP on the client, but the server.py seems to not receive any data. What am I doing wrong here?
The example code is something like this (it works for local networks, but on separate PCs only, IP address, of course, is just an example and probably will not work for anyone else):
server.py:
import socket
import time
import pickle
IP = "192.168.1.69"
PORT = 1234
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen()
print(f'Listening for connections on {IP}:{PORT}...')
client_socket, client_adress = server_socket.accept()
print(pickle.loads(client_socket.recv(1024)))
client_socket.send(pickle.dumps('DONE'))
time.sleep(1)
client_socket.send(pickle.dumps('boi'))
print(client_socket, client_adress)
client.py:
import socket
import pickle
import time
IP = "192.168.1.69"
PORT = 1234
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.setblocking(False)
client_socket.send(pickle.dumps("NICE."))
time.sleep(1)
print(pickle.loads(client_socket.recv(1024)))
time.sleep(1)
print(pickle.loads(client_socket.recv(1024)))
Thank you.

What do I do to make this socket to a websocket?

I've got this code at the moment, it's a simple socket server:
import socket
import time
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
serversocket.bind((host, port))
serversocket.listen(5)
while True:
clientsocket,addr = serversocket.accept()
print(str(addr) + "connected")
test = "Package"
clientsocket.send(test.encode('utf-8'))
clientsocket.close()
I also made a client that gets the message. However, how do I make it so that when I type in the address of my socket on for example Chrome, it displays "Package". I have basic knowledge on handlers and such, but I can't find any DIY websocket tutorials on the internet.
I do not want to use for example tornado, I want to make a simple one myself
Thank you very much in advance!

Why can I only connect to myself through socket? (python)

I'm trying to make simple Client-Server program but it's only working when I'm running both scripts on same computer, when moving it to other computer - It does not make connection at all.
Server:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host="0.0.0.0" # Bind with everyone
port = 13254 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close() # Close the connection
Client:
__author__ = 'user-pc'
import socket # Import socket module
s = socket.socket() # Create a socket object
host = "192.168.10.4" # Server Ip
port = 13254 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
Can you please help me to figure the problem?
Thanks.
As it works when you run both scripts on the server it is likely your port is closed.
Verify your port is open. Go to this website: http://www.canyouseeme.org/
Enter your port and IP. It is highly likely your port is closed. This will verify that. Assuming your port is open you need to look into your firewall settings as reccomended by dmg in the comment above. Then, it is no longer a python issue!

Categories

Resources