0

I have a specific HTTP server that accepts incoming connection and keeps it opened, send data from time to time, like chat messages.

I tried to use the following code:

#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-

import urllib2

url = "http://localhost/chat"

if __name__ == "__main__":
    for line in urllib2.urlopen(url):
        print line

That should print chat messages as soon as they arrive until user terminate the script. In fact this script waits 1 minute, than prints every message arrived during this minute, and exit.

Is there a way to force python to keep connection opened and print received data immediately?

Anthony
  • 12,407
  • 12
  • 64
  • 88

1 Answers1

2

As far as I know urllib2 maintains its buffer, and you can’t do what you want using urllib2 (in documented way, at least). You’d better directly use httplib.

minhee
  • 5,688
  • 5
  • 43
  • 81