Server :
HOST = 'localhost'
PORT = 8000
Client :
HOST = '#MyIPAddress' (found on http://www.ip-adress.com/)
PORT = 8000
Client fails connecting to the server. It works if client.HOST = 'localhost' to but that's not the point. I forwarded my router to open that port. I want it to work over the internet. I tried stuff like https://pagekite.net/ but I couldn't make it work.
I would like to see any complete "hello world" example wich would send "hello world" through the internet between two clients. Either server runs on my computer or on a hosted platform I would like to see a full concrete example.
If your server and client are located at the same machine, then just
set client.HOST = 'localhost' as you said.
If your server and client are located at different machines, you
should try ping through each other.
If your server is behind an NAT device, you need to enable port
forwarding (such as upnp, dmz etc.) in order to make sure tcp
request from your client could reach your server first. What your
said sounds more like a network issue than a programming issue. Hope
this could help you.
Related
I am trying to make a network between some computers in python and I want to make it so that I can have another computer send information to my IP address and on a set port. Which my computer can then receiving from just listening on that port not know what IP the information has come from.
(I do not know if this is possible but any help would be amazing)
resv_conection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
resv_conection.connect(IP, PORT) # I don't want it to listen to the a specific IP, just any on that port.
I am using socket library in python.
Thank you very much,
a server must have an IP, even its 127.0.0.1 which is localhost.
The server is listening on a Port for a TCP Connection.
If you are not on the same computer, one need to check the IP for the Server.
The Server must be in same subnetwork and the IP/Port must not be blocked by a firewall.
Than the Client can connect via TCP Socket to this IP Address on a specific port.
See: https://realpython.com/python-sockets/
Use the Echo Server and Echo Client
test it locally first
after success play with the PORT see it works with changes
check the IP of the Server PC (with: "ip a" for example)
and run the Client on a different PC with changeging HOST.
I'm creating an extremely simple Vega visualization viewer: it's a one file module that serves a base HTML page containing just the Vega graphic and an HTML5 EventSource of updates. The user (me) is working in a Python shell through ssh, creates an object representing the viewer, which prints its IP and port for the user to paste into their (my) web browser. This HTTP server doesn't serve files or take input from clients, so I don't see any security concerns.
The part I'm unsure of is how to set (host, port) such that my web browser can find the HTTP server running in the remote Python. I've been experimenting all afternoon, and I don't know if I'm misunderstanding what's supposed to happen or if the servers I use have changed their access policies.
Here's a minimal example:
import SimpleHTTPServer
import SocketServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((host, port), Handler)
print(httpd.server_address)
httpd.serve_forever()
If I'm running this locally and want to ensure that outside viewers cannot access it, do I set host to "127.0.0.1" because that means a client would have to access it as 127.0.0.1, which can only happen locally? In this case, port can be 0 to get any open port.
If I'm running this remotely want to to ensure that outside viewers can access it, do I set host to "" or "0.0.0.0" because that means that a client can access it as any address that makes its way to the server? In this case, I might not be able to set port to 0 because many of those ports might be blocked, or is the OS smarter about this?
Basically, how is access control in Python's SocketServer supposed to work?
This is basic TCP. Nothing to do with Python.
If you listen at 127.0.0.1, only clients running in the same host can connect.
If you listen at 0.0.0.0, anybody can connect, firewalls permitting.
Why is my python script behaving this way?
I give it the instruction to connect via port 7777 but instead it is going over 45604.
I am NOT using socket.bind((socket.gethostname(),port))
Instead I work either with socket.bind(("0.0.0.0",port))
or with socket.bind(("127.0.0.1",port))
so I'm working local here. Why does my computer reroute the ports?
There should be no need for that, shouldn't it? Can I somehow disable it locally?
I am answering in the absence of any of your actual code.. So I have to make assumptions here:
1) You have server (right side in picture) listening on port 7777.
2) You are running a client on the same machine (left side of picture) that is connecting to the server.
So, the client (on the left shell) is connecting to the server (right shell window). The server is listening on 7777 and the client is connecting to the server from 45604 (client and server cannot occupy the same port on the same machine!)
Put another way, the client is "sending" to port 7777 from port 45604. Maybe that makes better sense?
A TCP connection is defined by 4 numbers: source IP address, source port, destination IP address, destination port.
The connection goes from 127.0.0.1 port 45604 to 127.0.0.1 port 7777.
The source port (45604) is a value chosen by the system from a wide range of unused ports (it is called an ephemeral port), because your program did not set a specific source port.
I have a server running by using python's base http server. The host name used is '127.0.0.1' the local host, and the port number is set to 8000. I have the public ip address of the computer operating this server.
If I wanted to send a http get request to this from another computer, what would I type into my browser?
Sounds like you've got your server process running on the wrong interface. 127.0.0.1 is not a hostname but an IP address, specifically the local loopback address. It is not reachable from any other machine (unless something's gone tragically wrong with your network configuration).
You can run anything you like on the 127.0.0.1 interface, and no one else can directly connect to it from a remote machine. That's pretty much the point --- it's for testing programs that use the Internet Protocol, and (in recent years) for starting single-user servers without worrying about security. (Python 2's SimpleHTTPServer does this, as do some personal wikis, and I think iPython Notebook.)
The public address for the host running your Web server is a completely unrelated network interface, with its own hardware and its own port 8000. It doesn't know or care that you've got something listening on some other interface's port 8000, so it should refuse attempts to connect to that port.
Since you didn't post any code, I have no idea what you need to change to get your server running on the correct interface. Assuming you've more or less followed the example in the BaseHTTPServer.HTTPServer docs:
def run(
server_class=BaseHTTPServer.HTTPServer,
handler_class=BaseHTTPServer.BaseHTTPRequestHandler,
):
server_address = ('', 8000) # <----= Replace the string.
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
That server_address tuple is a string containing the IP address ('1.2.101.202' or whatever), followed by an integer port number. So replace the string with your host machine's public-facing IP address.
Note that port 8000 is outside the reserved range (0 up to but not including 1024), so it's possible that some unrelated service is already using that port. (Numerous applications are already squatting port 8000.) If so, you'll just have to choose another port number. You can chose anything from 1024 up to but not including 65536, but as with 8000, someone else might already be using it.
Depending on your operating system and its security setup, you might not have permission to open a socket that listens on an arbitrary port number. If so, that's between you and your ISP or sysadmin.
http://yourip:port/func
yourip is your public ip.
port is 8080
func is your registered function.
and also make sure you port is opened
I recently wrote a client/server pair in python using sockets,but the problem is client doesn't connect to server on another network.I've tried port forwarding and making internal IP address static, a question which really bother's me is do i need External/Public IP address to make the client connect and if this is the case what to do when the ISP changes my External IP address. Please give some suggestions,thanks.
code:
PORT=8888
srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
srvsock.bind( ('', PORT) )
srvsock.listen( 10 )
print 'server now listening on PORT '+str(PORT)
while 1:
clisock, (remhost, remport) = srvsock.accept()
dl_information_file="server.txt"
if os.path.exists(dl_information_file):
f=open('server.txt','rb')
read=f.read()
clisock.send( read )
f.close()
First of all, try to run both server and client from the same computer (connect to localhost)
If that works your problem is port forwarding related.
see: how to portforward
Are the client and server on a local network or is the client and server separated by the Internet?
If the server is running on a machine behind a NAT, you will have to do portforwarding on that and make sure that the machine's IP is static or that you update the client with the updated IP.
If the server is within your own network, you can use socket.gethostbyname(socket.getfqdn()) to get the IP of your interface (careful, you may have more than one) and use that IP to bind a socket.
You may also use WireShark to troubleshoot the connection - you can see what packets are making their way out of your client and what packets are making their way to your server.
Without further code and more information on the network, it's really hard to say any more - it could be the firewall, it could be the NAT, it could be a badly configured interface.
edit: It appears that you're binding the socket to '', which means it should be bound to the localhost port, meaning that the server listens for connections on the local loopback (127.0.0.1). This interface is not accessible to any other machine that the one which the server is running on.
You should use either a statically configured IP in a variable in your script, or that socket.gethostbyname(socket.getfqdn())).