0

I'm a total beginner and newb at all this programming stuff and I'm trying to learn as best as I can but I'm having issues. Can anyone explain Python networking? Where I'm getting confused is my book says:

"You can run several clients while the server is still running. By replacing the call to gethostname in the client with the actual host name of the machine where the server is running, you can have the two programs connect across a network from one machine to another."

I'm having trouble understanding the part with the asterisks surrounding it. I don't know what they mean by that.

#Client Code:
#!/usr/bin/env python
import socket

s = socket.socket()

host = socket.gethostname()
port = 1234

s.connect((host, port))
print s.recv(1024)



#Server code:
#!/usr/bin/env python
import socket

s = socket.socket()

host = socket.gethostname()
port = 1234
s.bind((host,port))

s.listen(5)
while True:
    c, addr = s.accept()
    print 'Got connection from', addr
    c.send('Thank you for connecting')
    c.close()

Here's the code that my book has but I don't really understand it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bryce Caro
  • 65
  • 10

1 Answers1

2

Presumably there's some line of code along the lines of:

socket.create_connection(socket.gethostname())

And they want you to either specify the ip address, something like:

socket.create_connection("192.168.1.1")

Or specify the hostname, something like:

socket.create_connection("my_hostname")
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • Does the "my_hostname" need to be a specific name or can you name it whatever you want? – Bryce Caro Jul 03 '15 at 02:00
  • "my_hostname" needs to be the hostname of the computer you want to connect to. For instance if you did "ping my_hostname" then if "my_hostname" were valid ping would be able to turn "my_hostname" into an IP, otherwise it would say "ping: unknown host my_hostname". – CrazyCasta Jul 03 '15 at 10:14