I cant make my python server run with my IPV 4 address - python

import socket
PORT = 5050
SERVER = "Insert my public IPV4 address here"
ADDR = (SERVER, PORT)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR) #here is the line I get the error for
The error says:
Traceback (most recent call last):
File "The Directory", line 15, in <module>
server.bind(ADDR)
OSError: [WinError 10049] The requested address is not valid in its context
Can I not use my IPV4 to make the server public?
If not then what do I use?

Related

WinError 10061 and WinError 10022 in socket programming only on Windows

I have a very simple Python code for bind or connect to a port. it works without any error on Ubuntu and CentOs but I have an error on Windows 10. I turned off the firewall and antivirus but it didn't help.
my code:
import socket
port = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
try:
s.connect((host,port))
except:
s.bind((host, port))
s.listen(1)
print("I'm a server")
clientsocket, address = s.accept()
else:
print("I'm a client")
error on windows 10:
Traceback (most recent call last):
File "win.py", line 11, in <module>
s.connect((host,port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During the handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "win.py", line 13, in <module>
s.bind((host, port))
OSError: [WinError 10022] An invalid argument was supplied
Edit:
I found my problem is in Try... Except part, if I put this code in two files my problem will solve. But Why? try except don't work correctly in Windows?
The connect() fails because there is no server socket listening at (host,port).
The bind() fails because you can't bind to a hostname, only to an IP address. Unless you want to listen on just a specific network interface, you should bind to 0.0.0.0 to listen on all interfaces.

UDP Socket using IPV6

Hello everyone, I am implementing a bittorrent client and i have hit a roadblock.
Here is my code to get the peer list from a particular tracker which only deals in ipv6.
tracker = "udp://tracker.birkenwald.de:6969/announce"
parse = urlparse(tracker)
ip, port = (socket.getaddrinfo(parse.hostname, parse.port, 0, 0, socket.SOL_TCP))[0][4][0], parse.port
#ip, port = 2001:1b10:1000:8101:0:242:ac11:2,6969
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))
#bytestringForConnecting() is a function for getting the byte version as written in the protocol.
Getting the following error
Traceback (most recent call last):
File "main.py", line 17, in <module>
t.get_peer_list()
File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 21, in get_peer_list
self.udp_request(url)
File "/home/aditya/Desktop/CN/CN PROJECT/tracker.py", line 64, in udp_request
sock.sendto(tracker_connection.bytestringForConnecting(), (ip, port))
socket.gaierror: [Errno -9] Address family for hostname not supported
The python socket documentation says that AF_INET6 addresses must be passed as a tuple of 4 values. If you pass a 2-tuple it'll be interpreted as AF_INET (IPv4) address.

NameError: name ??? is not defined - input python 3.7.6

To practice network programming I am trying to set up a client and server, the client can send a message to the server. However, when trying to get input from user on client, it gets a NameError. I looked this error up and saw it used to pop up before python 3, instead of raw_input. However, I am using python 3.7.6 so I don't understand what could be wrong. Any help is appreciated!
Client code:
import socket
SERVER = socket.gethostbyname(socket.gethostname())
PORT = 5050
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
SIZE = 1024
DISCONNECT_MESSAGE = "DISCONNECT"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
client.send(message)
print(client.recv(SIZE).decode(FORMAT))
text = input("Type your message:")
send(text)
send(DISCONNECT_MESSAGE)
Traceback:
Traceback (most recent call last):
File "client.py", line 18, in <module>
text = input("(To disconnect type: 'DISCONNECT')\nType your message:")
File "<string>", line 1, in <module>
NameError: name 'hoi' is not defined

Socket Error: 111 [.connect((host, port))]

I am doing a course and it is teaching me socket right now but this code they are showing is not working for me?
import socket
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "localhost"
port = 5000
s.connect((host, port))
print("It works!")
if __name__ == "__main__":
main()
Error:
Traceback (most recent call last):
File "create_connection.py", line 14, in <module>
main()
File "create_connection.py", line 9, in main
s.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused
EDIT:
On this video there is nothing listening but there is no error?
Connect request failed with connection refused.
You need a TCP server which is listening on 5000 port.
There should be a server waiting for connection in port 5000, only then it will work. Otherwise this error is expected

How to connect to a socket server in my network?

After doing port forwarding on port 50000 on my router now i can connect to my server using a computer from another network :
SERVER CODE :
import socket
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.bind(("0.0.0.0" , 50000))
s.listen(5)
c , a = s.accept()
CLIENT CODE (that is connected to another network) :
import socket
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.connect(("my_public_ip" , 50000))
but the problem is that if i try to connect from my network to the server that is on my network (after doing port forwarding) i get an error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Categories

Resources