Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Python help: Sockets
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Chittle




PostPosted: Mon Nov 17, 2008 4:58 pm   Post subject: Python help: Sockets

Hello I have been working for the last few days on networking and sockets in python for my Final Project for school. While I am working on the code at home it works perfectly but when i get to school the client is not able to connect to the host, but at school I am able to connect to the same computer that I am on, it only doesn't work when i am trying to get it to connect to seperate computers. so I was just wondering if anybody had any ideas. Heres the code

code:

import socket
import time
class myNetwork:
    def __init__(self):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.host = socket.gethostname()
        self.port = 4002
       
    def isHost(self):
        self.s.bind((socket.gethostname(), self.port))
        self.s.listen(5)
        print "Ip address:", socket.gethostbyname(socket.gethostname())

    def client (self):
        self.host = raw_input("Please enter the IP address of the host: ")
        if self.host == "":
            self.host = "192.168.0.103"
        self.s.connect((self.host, self.port))

    def connect(self):
        self.conn, self.addr = self.s.accept()
        print "Connected"
        print "Client is at", self.addr


net = myNetwork()
hostClient = raw_input("Is this a host or a client? ")
if hostClient.lower() == "host":
    net.isHost()
    net.connect()
    data = raw_input("Please enter a message: ")
    net.conn.sendall(data)
else:
    net.client()
    data = net.s.recv(1000000)
    print data
    print "received", len(data), "bytes"
time.sleep(5)
net.conn.close()


I would like to point out that I have no 'evil' intent for networking at school I just want my Final Project to work. A thank you in advance to any help

Moderation: please modify your original code when someone suggests a way to preserve code formatting from now on. - wtd
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Mon Nov 17, 2008 7:56 pm   Post subject: RE:Python help: Sockets

Have you run into this situation using multiple computers? If it's a far away machine it could be on a different subnet and not be configured to route packets between them.

Just a complete stab in the dark.

Oh, and it's generally advisable to use either [ syntax = "python" ] [ /syntax ] or [ code ] [ /code] tags, otherwise your code will not be displayed with the correct font and the indentation will not be preserved. This normally helps with readability, but in Python indents are required Smile.
Zeroth




PostPosted: Mon Nov 17, 2008 8:01 pm   Post subject: Re: Python help: Sockets

This is actually more of a networking issue. What kind of code does the host have on its end? Or the client? Second of all, you should understand there may be firewalls preventing your communications. You should talk to your teacher about the problem you are facing.

I do notice that in one spot you hard-code the IP address, and this may be an issue too.
Chittle




PostPosted: Mon Nov 17, 2008 8:08 pm   Post subject: Re: Python help: Sockets

Okay thanks for replying guys this is actually my first topic ever on compsci.ca so thanks for explaining how to make my code look nice
I would like to point out a few things about my code: This code works for both the client or the host it asks which it is as soon as it is run and then runs the approapriate code after that
and also that the hard coded IP address is my computer which it only changes to that if I don't type in anything for the IP address when it asks for it (Whenever i get lazy) Smile


And this problem only occurs at school and the computer are not far away from eachother at all they are actually right next to eachother.
Chittle




PostPosted: Mon Dec 08, 2008 7:30 pm   Post subject: Re: Python help: Sockets

Problem solved: I booted ubuntu off of my flash drive and as long as that computer is the host there are no problems with connections
andrew.




PostPosted: Tue Dec 30, 2008 8:07 pm   Post subject: RE:Python help: Sockets

So it was probably some kind of school computer firewall, right? Some program the school board installed on the computer?
Chittle




PostPosted: Sun Jan 11, 2009 9:03 pm   Post subject: Re: Python help: Sockets

Yeah im guessing it was some program that booted up on windows when I logged in.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: