Forums

Socket program

Hi, I am running a server program in pythonanywhere.com. It receives the client connection and replies back with welcome and disconnects the connection. The server is running correctly. But in client program using what address i need to connect.? Can someone please help me out.

Here is the source code of server :

import socket
s = socket.socket()
s.bind(('',45342))
s.listen(1)
while True:
    c, addr = s.accept()
    print 'got connection from ',addr
    c.send('Thank you!')
    c.close()
print 'hello world!'

Client:

import socket
s= socket.socket()
s.connect(('_what to put here?_',45342))
print s.recv(1024)
s.close()

[edit by admin: formatting]

Unfortunately raw socket servers won't work on PythonAnywhere, we only support HTTP servers that use the WSGI protocol to connect to the network.

what about clients, why does it deny connection to remote servers/sites...

for example, facebook is in the whitelist, but connecting to it only return name or service not known

import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("http://facebook.com",80))
#or socket.gethostbyname("http://facebook.com")

all of the above returns giaer error, how would I go about it?

Outbound connections from free accounts need to go through a proxy server -- that is what manages the whitelist. So if you want to make a connection to a whitelisted server, you either need to write your own proxy-aware networking code (which can be tricky) or use an HTTP/HTTPS library that handles that for you, like requests. We recommend the latter.